Skip to content Skip to sidebar Skip to footer

How To Add Padding For Background Image

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 whole width? I

Solution 1:

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"?><insetxmlns: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:

<LinearLayoutxmlns: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">

Solution 2:

Ur background drawable should be like this,using android:bottom

<?xml version="1.0" encoding="utf-8"?><layer-listxmlns:android="http://schemas.android.com/apk/res/android"><itemandroid:bottom="48dp"android:drawable="@drawable/bg"></item></layer-list>

Solution 3:

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.

<LinearLayoutxmlns: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"><LinearLayoutandroid: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.

Solution 4:

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">

Post a Comment for "How To Add Padding For Background Image"