简体   繁体   中英

Set up a LinearLayout into an other LinearLayout with imagebackground

I would like to set a background of my main layout and put content into it. The problem: The content is set up to fill the whole background but it is completely compressed into a few pixels.

UPDATE: I used a 9-patch image for my background.

Here is my code:

    <LinearLayout
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"  
    android:gravity="center"
    android:background="@drawable/badgeandroid"
    android:layout_marginLeft="20sp"
    android:layout_marginRight="20sp"
    android:layout_marginBottom="20sp"
    >


    <LinearLayout 
    android:id="@+id/contentbadge"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"  
    android:gravity="center"
    android:layout_margin="10sp"
    >

   <TextView 
    android:id="@+id/text"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"   
    android:text="Your badge pocket is empty"
    android:textColor="#FFFFFF"
    android:textSize="25sp"
    android:textStyle="bold" 
    android:gravity="center"
   ></TextView>

    </LinearLayout >

    </LinearLayout >

Your layout margins aren't correct. You are using "sp"-> scaled pixels. This is the dimensions for texts. What you should be using is "dp" or "dip" (both the same) -> Density independent pixels.

Ok, I finally found the solution. The 9 patch screw up everything. Answer here .

I just add removed my padding and include them into the 9patch:

      <LinearLayout
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"  
    android:gravity="center"
    android:background="@drawable/badgeandroid"
    android:layout_margin="0dp"
    >


    <LinearLayout 
    android:id="@+id/contentbadge"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"  
    android:gravity="center"
    android:layout_margin="10sp"
    >

   <TextView 
    android:id="@+id/text"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"   
    android:text="Your badge pocket is empty"
    android:textColor="#FFFFFF"
    android:textSize="25sp"
    android:textStyle="bold" 
    android:gravity="center"
   ></TextView>

    </LinearLayout >

    </LinearLayout >

Romain Guy explains why here .

Try with this code

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_marginBottom="20dip"
    android:layout_marginLeft="20dip"
    android:layout_marginRight="20dip"
    android:gravity="center"
    android:orientation="vertical" >

    <LinearLayout
        android:id="@+id/contentbadge"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_margin="10dip"
        android:gravity="center"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="Your badge pocket is empty"
            android:textColor="#FFFFFF"
            android:textSize="25dip"
            android:textStyle="bold" >
        </TextView>
    </LinearLayout>

</LinearLayout>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM