Style bottom Line in Android

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

    It's kind of a hack, but I think this is probably the best way to do it. The dashed line will always be on the bottom, regardless of the height.

    <layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    
        <item>
            <shape android:shape="rectangle" >
                <solid android:color="#1bd4f6" />
            </shape>
        </item>
    
        <item android:top="-2dp" android:right="-2dp" android:left="-2dp">
            <shape>
                <solid android:color="@android:color/transparent" />
                <stroke
                    android:dashGap="10px"
                    android:dashWidth="10px"
                    android:width="1dp"
                    android:color="#ababb2" />
            </shape>
        </item>
    
    </layer-list>
    

    Explanation:

    The second shape is transparent rectangle with a dashed outline. The key in making the border only appear along the bottom lies in the negative margins set the other sides. These negative margins "push" the dashed line outside the drawn area on those sides, leaving only the line along the bottom. One potential side-effect (which I haven't tried) is that, for views that draw outside their own bounds, the negative-margin borders may become visible.

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

    I think you do not need to use shape if I understood you.

    If you are looking as shown in following image then use following layout.

    enter image description here

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
    
    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:background="#1bd4f6"
        android:paddingBottom="4dp" >
    
        <TextView
            android:layout_width="200dp"
            android:layout_height="wrap_content"
            android:background="#ababb2"
            android:padding="5dp"
            android:text="Hello Android" />
     </RelativeLayout>
    
     </RelativeLayout>
    

    EDIT

    play with these properties you will get result

        android:top="dimension"
        android:right="dimension"
        android:bottom="dimension"
        android:left="dimension"
    

    try like this

    <?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="#1bd4f6" />
        </shape>
    </item>
    
    <item android:top="20px"
        android:left="0px">
        <shape android:shape="line"  >
            <padding android:bottom="1dp" />
    
            <stroke
                android:dashGap="10px"
                android:dashWidth="10px"
                android:width="1dp"
                android:color="#ababb2" />
        </shape>
    </item>
    
    </layer-list>
    
    0 讨论(0)
提交回复
热议问题