Style bottom Line in Android

后端 未结 14 2450
独厮守ぢ
独厮守ぢ 2020-11-28 17:53

I need to create an android shape so that only the bottom has stroke (a dashed line). When I try the following, the stroke bisects the shape right through the center. Does a

相关标签:
14条回答
  • 2020-11-28 18:24

    easiest way to do this is put after that view where you want bottom border

        <?xml version="1.0" encoding="utf-8"?>
        <View
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="@color/colorPrimary" />
    
    0 讨论(0)
  • 2020-11-28 18:26
    <?xml version="1.0" encoding="utf-8"?>
    <layer-list xmlns:android="http://schemas.android.com/apk/res/android">
        <item
            android:top="-6dp"
            android:left="-6dp"
            android:right="-6dp"
            android:bottom="0dp">
    
            <shape android:shape="rectangle">
                <solid android:color="#88FFFF00"/>
                <stroke
                    android:width="5dp"
                    android:color="#FF000000"/>
            </shape>
        </item>
    </layer-list>
    
    0 讨论(0)
  • 2020-11-28 18:27

    I feel it is straightforward, without all this negative paddings or storks.

    <layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    
        <item android:drawable="@color/main_bg_color"/>
        <item android:gravity="bottom">
            <shape android:shape="rectangle">
                <size android:height="5dp"/>
                <solid android:color="@color/bottom_bar_color"/>
            </shape>
        </item>
    </layer-list>
    
    0 讨论(0)
  • 2020-11-28 18:27

    A Simple solution :

    Create a drawable file as edittext_stroke.xml in drawable folder. Add the below code:

    <?xml version="1.0" encoding="utf-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android"
        android:shape="line"
       >
        <stroke
            android:width="1dp"
            android:color="@android:color/white" >
        </stroke>
    </shape>
    

    In layout file , add the drawable to edittext as

    android:drawableBottom="@drawable/edittext_stroke"

    <EditText
          android:textColor="@android:color/white"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:drawableBottom="@drawable/edittext_stroke"
          />
    
    0 讨论(0)
  • 2020-11-28 18:34

    Try next xml drawable code:

        <layer-list>
            <item android:top="-2dp" android:right="-2dp" android:left="-2dp">
                <shape>
                    <solid android:color="@android:color/transparent" />
                    <stroke
                        android:width="1dp"
                        android:color="#fff" />
                </shape>
            </item>
        </layer-list>
    
    0 讨论(0)
  • 2020-11-28 18:34

    Usually for similar tasks - I created layer-list drawable like this one:

    <layer-list xmlns:android="http://schemas.android.com/apk/res/android">
        <item>
            <shape android:shape="rectangle">
            <solid android:color="@color/underlineColor"/>
            </shape>
        </item>
    <item android:bottom="3dp">
        <shape android:shape="rectangle">
            <solid android:color="@color/buttonColor"/>
        </shape>
    </item>
    

    The idea is that first you draw the rectangle with underlineColor and then on top of this one you draw another rectangle with the actual buttonColor but applying bottomPadding. It always works.

    But when I needed to have buttonColor to be transparent I couldn't use the above drawable. I found one more solution

    <layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape android:shape="rectangle">
            <solid android:color="@android:color/transparent"/>
        </shape>
    </item>
    
    <item android:drawable="@drawable/white_box" android:gravity="bottom"     android:height="2dp"/>
    </layer-list>
    

    (as you can see here the mainButtonColor is transparent and white_box is just a simple rectangle drawable with white Solid)

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