0

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 >
user420574
  • 1,437
  • 5
  • 21
  • 33

3 Answers3

0

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.

Frank Sposaro
  • 8,511
  • 4
  • 43
  • 64
0

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.

Community
  • 1
  • 1
user420574
  • 1,437
  • 5
  • 21
  • 33
0

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>
AITAALI_ABDERRAHMANE
  • 2,499
  • 1
  • 26
  • 31