我有一个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?


当前回答

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

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

用下面的代码创建一个名为/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平台上进行测试

其他回答

只是把我的解决方案添加到列表中。

我想要一个半透明的底部边界延伸到原来的形状(所以半透明的边界在父矩形之外)。

<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
  <item>
    <shape android:shape="rectangle" >      
      <solid android:color="#33000000" /> <!-- Border colour -->
    </shape>
  </item>
  <item  android:bottom="2dp" >
    <shape android:shape="rectangle" >     
      <solid android:color="#164586" />
    </shape>
  </item>
</layer-list>

这给了我;

所以我想做一些稍微不同的事情:在底部唯一的边界,模拟一个ListView分隔符。我修改了Piet Delport的答案,得到了这个:

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

注意使用px而不是dp来获得恰好1像素的分隔线(一些手机DPIs会使1dp线消失)。

尝试用线性布局包装图像,并将其背景设置为你想要的文本周围的边框颜色。然后设置在textview的填充物的厚度,你想为你的边界。

// Just simply add border around the image view or view

<ImageView
                android:id="@+id/imageView2"
                android:layout_width="90dp"
                android:layout_height="70dp"
                android:layout_centerVertical="true"
                android:layout_marginRight="10dp"
                android:layout_toLeftOf="@+id/imageView1"
                android:background="@android:color/white"
                android:padding="5dip" />

// After that dynamically put color into your view or image view object

objView.setBackgroundColor(Color.GREEN);

//VinodJ/Abhishek

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

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

用下面的代码创建一个名为/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平台上进行测试