tags:

views:

134

answers:

1

Hi,

I have a linear layout which has a background image (a 9 patched png file). How can I add padding to left and right so that the background image does not take up the whole width? I have tried 'android:paddingLeft' and 'android:paddingRight', but that does not change anything.

<LinearLayout  xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingLeft="25dip"
android:paddingRight="25dip"
android:background="@drawable/background">

The whole background still stretches the whole screen width.

Thank you for any help.

A: 

That doesn't work because padding only acts on the contents of the LinearLayout. By using a second LinearLayout inside this one the padding will take effect. You must define the background color of the first LinearLayout that will be visible in the padding area.

<LinearLayout  xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingLeft="25dip"
android:paddingRight="25dip"
android:background="#FF000000">

<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/background">

</LinearLayout>

</LinearLayout>

Note: This is probably also possible by using an XML file for the background.

stealthcopter