Style bottom Line in Android

后端 未结 14 2451
独厮守ぢ
独厮守ぢ 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:34

    A Simple solution :

    <?xml version="1.0" encoding="utf-8"?>
    <layer-list xmlns:android="http://schemas.android.com/apk/res/android">
        <item
            android:bottom="-1dp"
            android:left="-1dp"
            android:right="-1dp"
            android:top="-1dp">
    
            <shape android:shape="rectangle">
    
                <solid android:color="#AARRGGBB" />
    
                <stroke
                    android:width="5dp"
                    android:color="@android:color/red"
                    android:dashWidth="10dp"
                    android:dashGap="12dp" />
    
            </shape>
        </item>
    </layer-list>
    

    And finally we have something like that :)

    0 讨论(0)
  • 2020-11-28 18:36

    use this xml change the color with your choice.

    <item>
        <layer-list>
            <item>
                <shape>
                    <solid android:color="@color/gray_500" />
    
                </shape>
            </item>
            <!-- CONTENT LAYER -->
            <item android:bottom="2dp" >
                <shape>
                    <solid android:color="#ffffff" />
                </shape>
            </item>
        </layer-list>
    </item>
    

    In Case if you want programmatically

    public static Drawable getStorkLineDrawable(@ColorInt int colorStrok, int    iSize, int left, int top, int right, int bottom)
    
    {
        GradientDrawable gradientDrawable = new GradientDrawable();
        gradientDrawable.setShape(GradientDrawable.RECTANGLE);
        gradientDrawable.setStroke(iSize, colorStrok);
        LayerDrawable layerDrawable = new LayerDrawable(new Drawable[]{gradientDrawable});
        layerDrawable.setLayerInset(0, left, top, right, bottom);
        return layerDrawable;
    }
    

    call this method like

      Drawable yourLineDrawable=  getStorkLineDrawable(yourColor, iSize, -iSize, -iSize, -iSize, 0);
    
    0 讨论(0)
  • 2020-11-28 18:37

    This does the trick...

    <item >
        <shape android:shape="rectangle">
            <solid android:color="#YOUR_BOTTOM_LINE_COLOR"/>
        </shape>
    </item>
    
    <item android:bottom="1.5dp">
        <shape android:shape="rectangle">
            <solid android:color="#YOUR_BG_COLOR"/>
        </shape>
    </item>
    
    0 讨论(0)
  • 2020-11-28 18:40

    it is completely transparent Edittext with transparent background.

    <item>
        <shape android:shape="rectangle" >
            <solid android:color="@color/transparent" />
        </shape>
    </item>
    
    <item android:top="-3dp" android:right="-3dp" android:left="-3dp">
        <shape>
            <solid android:color="@android:color/transparent" />
            <stroke
    
                android:width="2dp"
                android:color="@color/bottomline" />
        </shape>
    </item>
    

    0 讨论(0)
  • 2020-11-28 18:42

    This worked best for me:

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

    Shows the line on the bottom only. You can easily change with stroke width to size you like and also update the top, left, right on the accordingly.

    0 讨论(0)
  • 2020-11-28 18:43

    This answer is for those google searchers who want to show dotted bottom border of EditText like here

    Create dotted.xml inside drawable folder and paste these

    <layer-list xmlns:android="http://schemas.android.com/apk/res/android">
        <item
            android:bottom="1dp"
            android:left="-2dp"
            android:right="-2dp"
            android:top="-2dp">
            <shape android:shape="rectangle">
                <stroke
                    android:width="0.5dp"
                    android:color="@android:color/black" />    
                <solid android:color="#ffffff" />
                <stroke
                    android:width="1dp"
                    android:color="#030310"
                    android:dashGap="5dp"
                    android:dashWidth="5dp" />
                <padding
                    android:bottom="5dp"
                    android:left="5dp"
                    android:right="5dp"
                    android:top="5dp" />
            </shape>
        </item>
    </layer-list>
    

    Then simply set the android:background attribute to dotted.xml we just created. Your EditText looks like this.

    <EditText
        android:id="@+id/editText"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Some Text"
        android:background="@drawable/dotted" />
    
    0 讨论(0)
提交回复
热议问题