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

后端 未结 24 2377
盖世英雄少女心
盖世英雄少女心 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:12

    You can also wrap the view in a FrameLayout, then set the frame's background color and padding to what you want; however, the textview, by default has a 'transparent' background, so you'd need to change the textview's background color too.

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

    Just to enforce @phreakhead ´s and user1051892 ´s answers, <item android:bottom|android:left|android:right|android:top> if negative, must to be greater than <stroke android:width>. If not, item´s painting will be mixed with stroke´s painting and you may think these values are not working.

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

    Simply add Views at the top and bottom of the View

    <?xml version="1.0" encoding="utf-8"?>
    <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <View
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:background="@color/your_color"
            app:layout_constraintBottom_toTopOf="@+id/textView"
            app:layout_constraintEnd_toEndOf="@+id/textView"
            app:layout_constraintStart_toStartOf="@+id/textView" />
    
        <TextView
            android:id="@+id/textView"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginTop="32dp"
            android:gravity="center"
            android:text="Testing"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />
    
        <View
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:background="@color/your_color"
            app:layout_constraintEnd_toEndOf="@+id/textView"
            app:layout_constraintStart_toStartOf="@+id/textView"
            app:layout_constraintTop_toBottomOf="@+id/textView" />
    
    </android.support.constraint.ConstraintLayout>
    
    0 讨论(0)
  • 2020-11-22 14:14

    Add file to res/drawable

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

    Add link on this file to background property

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

    In android 2.2 you could do the following.

    Create an xml drawable such as /res/drawable/textlines.xml and assign this as a TextView's background property.

    <TextView
    android:text="My text with lines above and below"
    android:background="@drawable/textlines"
    />
    

    /res/drawable/textlines.xml

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

    The down side to this is that you have to specify an opaque background colour, as transparencies won't work. (At least i thought they did but i was mistaken). In the above example you can see that the solid colour of the first shape #FFffffdffffd is copied in the 2nd shapes stroke colour.

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

    So I wanted to do something slightly different: a border on the bottom ONLY, to simulate a ListView divider. I modified Piet Delport's answer and got 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="@color/background_trans_light" />    
    
            </shape>
       </item>
    
        <!-- this mess is what we have to do to get a bottom border only. -->
       <item android:top="-2dp"
             android:left="-2dp"
             android:right="-2dp"
             android:bottom="1px"> 
          <shape 
            android:shape="rectangle">    
                <stroke android:width="1dp" android:color="@color/background_trans_mid" />
                <solid android:color="@null" />
            </shape>
       </item>
    
    </layer-list>
    

    Note using px instead of dp to get exactly 1 pixel divider (some phone DPIs will make a 1dp line disappear).

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