How to get gravity 'bottom' working on a drawable in xml

前端 未结 7 1481
南方客
南方客 2021-02-12 15:41

I have a simple aim. I want a light grey background on my FrameLayout with a black dividing line underneath it (only undernearth, not all around). So far I have this:

<         


        
相关标签:
7条回答
  • 2021-02-12 16:20

    I'd recommend using a nine-patch image for this - here's an example that should do the job:

    Example nine-patch image

    (It's only tiny but it is there!)

    If you place this in your drawable folder and set your FrameLayout's background to it then you should get the desired result. The content will go in the grey area and the #010101 pixel will be stretched horizontally to form a line at the bottom. Just make sure to keep the .9.png extension to ensure it gets loaded as a nine-patch.

    Hope that helps you

    0 讨论(0)
  • 2021-02-12 16:26

    This works!

    <item>
          <bitmap
             android:gravity="left|bottom"
             android:src="@drawable/myDrawable" />
     </item>
    
    0 讨论(0)
  • 2021-02-12 16:29
    <?xml version="1.0" encoding="utf-8"?>
    <layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
    
        <item>
            <shape android:shape="rectangle" >
                <solid android:color="@color/Black" />
            </shape>
        </item>
        <item android:bottom="5dp">
            <shape android:shape="rectangle" >
                <solid android:color="@color/White" />
            </shape>
        </item>
    
    </layer-list>
    
    0 讨论(0)
  • 2021-02-12 16:33

    Unfortunately I have not found a way to get the gravity working for drawables, but I found how you can achieve what you want with pure xml:

    <?xml version="1.0" encoding="utf-8"?>
    <layer-list xmlns:android="http://schemas.android.com/apk/res/android">
        <item
            android:top="-2dp"
            android:left="-2dp"
            android:right="0dp"
            android:bottom="1dp"
            >
            <shape android:shape="rectangle">
                <solid android:color="#EEEEEE"/>
                <stroke android:width="1dp" android:color="#010101"/>
            </shape>
        </item>
    </layer-list>
    

    Good luck:)

    0 讨论(0)
  • 2021-02-12 16:33

    Well, it's pretty old question, but none of the answers seem to answer the actual question (how to make gravity working) so here's a working example:

    <?xml version="1.0" encoding="utf-8"?>
    <layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    
        <item>
            <color android:color="#2196F3"/>        <!-- Background color -->
        </item>
    
        <item android:gravity="bottom">
            <shape>
                <size android:height="10dp" />      <!-- I would suggest 1dp -->
                <solid android:color="#F50057" />   <!-- Line color -->
            </shape>
        </item>
    </layer-list>
    

    By analogy, if you want to use gravity left or right change attribute height to width

    0 讨论(0)
  • 2021-02-12 16:40

    I've struggled a lot with this as well. Basically, to create a border at the bottom, you have to think in opposite direction from what you expect. You start with a rectangle with your Border color. On top of that, you draw the actual color, with an offset from the bottom. I use this:

    <?xml version="1.0" encoding="utf-8"?>
    <layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
    
        <!-- CREATE A RECTANGLE WITH YOUR BORDER COLOR -->
        <item>
            <shape android:shape="rectangle" >
                <solid android:color="#fff"/>
            </shape>
        </item>
    
        <!-- NOW DEFINE THE NORMAL COLOR-->
        <item android:bottom="BOTTOM_BORDER_THICKNESS E.G. 4dp">
            <shape android:shape="rectangle" >
                <solid android:color="#ccc"/>
            </shape>
        </item>
    </layer-list>
    

    Or, as Juan Pablo Saraceno suggested :

    <layer-list xmlns:android="http://schemas.android.com/apk/res/android" > 
        <item> 
            <color android:color="YOUR_BORDER_COLOR" /> 
        </item> 
        <item android:top="YOUR_BORDER_THICKNESS"> 
            <color android:color="YOUR_BG_COLOR" /> 
        </item> 
    </layer-list>
    
    0 讨论(0)
提交回复
热议问题