我有一个TextView,我想添加一个黑色边界沿其顶部和底部边界。我尝试添加android:drawableTop和android:drawableBottom到TextView,但这只会导致整个视图变成黑色。

<TextView
    android:background="@android:color/green"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:drawableTop="@android:color/black"
    android:drawableBottom="@android:color/black"
    android:text="la la la" />

有没有一种方法可以轻松地添加一个顶部和底部边界的视图(特别是TextView)在Android?


当前回答

在android 2.2中,您可以执行以下操作。

创建一个xml可绘制对象,如/res/drawable/textlines.xml,并将其指定为TextView的背景属性。

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

xml / res / drawable - textlines。

<?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>

这样做的缺点是你必须指定一个不透明的背景颜色,因为透明度是行不通的。(至少我是这么认为的,但我错了)。在上面的例子中,你可以看到第一个形状#FFdddddd的纯色被复制到了第二个形状的描边颜色中。

其他回答

首先创建一个内容如下所示的xml文件,并将其命名为border.xml,并将其放置在res目录中的布局文件夹中

<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <stroke android:width="1dp" android:color="#0000" />
    <padding android:left="0dp" android:top="1dp" android:right="0dp"
        android:bottom="1dp" />
</shape>

之后在代码里面使用

TextView tv = (TextView)findElementById(R.id.yourTextView);
tv.setBackgroundResource(R.layout.border);

这将使一个黑线的顶部和底部的TextView。

为了只在底部添加1dp白色边框,并且有一个透明的背景,你可以使用下面的方法,这比这里的大多数答案都简单。

对于TextView或其他视图添加:

android:background="@drawable/borderbottom"

在可绘制目录中添加以下XML,称为borderbottom.xml

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

如果你想在顶部设置边框,将android:top="-2dp"改为android:bottom="-2dp"

颜色不需要是白色的,背景也不需要是透明的。

固体元素可能不是必需的。这取决于你的设计(感谢V. Kalyuzhnyu)。

基本上,这个XML将使用矩形形状创建边框,但随后将顶部、右侧和左侧推到形状的渲染区域之外。这样只留下底部边界可见。

添加文件到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>

将此文件上的链接添加到后台属性

在android 2.2中,您可以执行以下操作。

创建一个xml可绘制对象,如/res/drawable/textlines.xml,并将其指定为TextView的背景属性。

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

xml / res / drawable - textlines。

<?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>

这样做的缺点是你必须指定一个不透明的背景颜色,因为透明度是行不通的。(至少我是这么认为的,但我错了)。在上面的例子中,你可以看到第一个形状#FFdddddd的纯色被复制到了第二个形状的描边颜色中。

目前接受的答案行不通。作为抗锯齿的结果,它在视图的左右两侧创建了薄的垂直边界。

这个版本工作得很完美。它还允许你独立地设置边界宽度,如果你愿意,你也可以在左边/右边添加边界。唯一的缺点是它不支持透明性。

用下面的代码创建一个名为/res/drawable/top_bottom_borders.xml的xml可绘制对象,并将其分配为TextView的background属性。

<?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="#DDDD00" /> <!-- border color -->
        </shape>
    </item>

    <item
        android:bottom="1dp" 
        android:top="1dp">   <!-- adjust borders width here -->
        <shape android:shape="rectangle">
            <solid android:color="#FFFFFF" />  <!-- background color -->
        </shape>
    </item>
</layer-list>

在Android KitKat和Marshmallow平台上进行测试