Is there an easy way to add a border to the top and bottom of an Android View?

后端 未结 24 2376
盖世英雄少女心
盖世英雄少女心 2020-11-22 13:36

I have a TextView and I\'d like to add a black border along its top and bottom borders. I tried adding android:drawableTop and android:drawableBottom

相关标签:
24条回答
  • 2020-11-22 14:15

    Write down below code

    <View
        android:layout_width="wrap_content"
        android:layout_height="2dip"
        android:layout_below="@+id/topics_text"
        android:layout_marginTop="7dp"
        android:layout_margin="10dp"
        android:background="#ffffff" />
    
    0 讨论(0)
  • 2020-11-22 14:17

    To change this:

    <TextView
        android:text="My text"
        android:background="@drawable/top_bottom_border"/>
    

    I prefer this approach in "drawable/top_bottom_border.xml":

    <?xml version="1.0" encoding="utf-8"?>
    <layer-list xmlns:android="http://schemas.android.com/apk/res/android">
        <item>
            <shape>
                <gradient
                    android:angle="270"
                    android:startColor="#000"
                    android:centerColor="@android:color/transparent"
                    android:centerX="0.01" />
            </shape>
        </item>
        <item>
            <shape>
                <gradient
                    android:angle="90"
                    android:startColor="#000"
                    android:centerColor="@android:color/transparent"
                    android:centerX="0.01" />
            </shape>
        </item>
    </layer-list>
    

    This only makes the borders, not a rectangle that will appear if your background has a color.

    0 讨论(0)
  • 2020-11-22 14:19

    Try wrapping the image with a linearlayout, and set it's background to the border color you want around the text. Then set the padding on the textview to be the thickness you want for your border.

    0 讨论(0)
  • 2020-11-22 14:23
    <shape xmlns:android="http://schemas.android.com/apk/res/android">
    
    <solid android:color="@color/light_grey1" />
    <stroke
        android:width="1dip"
        android:color="@color/light_grey1" />
    
    <corners
        android:bottomLeftRadius="0dp"
        android:bottomRightRadius="0dp"
        android:topLeftRadius="5dp"
        android:topRightRadius="5dp" />
    
        </shape>
    
    0 讨论(0)
  • 2020-11-22 14:28

    I've used a trick so that the border is displayed outside the container. With this trick only a line is drawn so the background will be shown of the underlying view.

    <?xml version="1.0" encoding="utf-8"?>
    <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="1dp"
                    android:color="#FF000000" />
    
                <solid android:color="#00FFFFFF" />
    
                <padding android:left="10dp"
                    android:right="10dp"
                    android:top="10dp"
                    android:bottom="10dp" />
            </shape>
        </item>
    
    </layer-list>
    
    0 讨论(0)
  • 2020-11-22 14:28
    <TextView
        android:id="@+id/textView3"
        android:layout_width="match_parent"
        android:layout_height="2dp"
        android:background="#72cdf4"
        android:text=" aa" />
    

    Just Add this TextView below the text where you want to add the border

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