I have a LinearLayout
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
Ur background drawable should be like this,using android:bottom
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:bottom="48dp" android:drawable="@drawable/bg">
</item>
</layer-list>
You can just use
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginLeft="25dip"
android:layout_marginRight="25dip"
android:background="@drawable/background">
You should use xml drawable, specially designed for this purposes - Inset Drawable: http://developer.android.com/guide/topics/resources/drawable-resource.html#Inset
For example:
res/drawable/inset_background.xml:
<?xml version="1.0" encoding="utf-8"?>
<inset xmlns:android="http://schemas.android.com/apk/res/android"
android:drawable="@drawable/your_background_image"
android:insetRight="25dip"
android:insetLeft="25dip" />
and then in your xml layout:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/inset_background">
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.