How to add padding for background image

后端 未结 4 959
野的像风
野的像风 2021-02-02 08:11

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

相关标签:
4条回答
  • 2021-02-02 08:32

    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>
    
    0 讨论(0)
  • 2021-02-02 08:33

    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">
    
    0 讨论(0)
  • 2021-02-02 08:43

    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">
    
    0 讨论(0)
  • 2021-02-02 08:52

    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.

    0 讨论(0)
提交回复
热议问题