有一个简单的方法来添加边框的一款Android观的顶部和底部? [英] Is there an easy way to add a border to the top and bottom of an Android View?

查看:219
本文介绍了有一个简单的方法来添加边框的一款Android观的顶部和底部?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个TextView,我想添加一个黑色的边框沿其顶部和底部边框。我尝试添加安卓drawableTop 安卓drawableBottom 的TextView的,但是这不仅造成整个视图变黑

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 to the TextView, but that only caused the entire view to become black.

<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" />

有没有一种方法可以轻松地在Android中增加一个顶部和底部边框视图(尤其是一个TextView)?

Is there a way to easily add a top and bottom border to a View (in particular, a TextView) in Android?

推荐答案

在Android 2.2的,你可以做到以下几点。

In android 2.2 you could do the following.

创建一个XML绘制如/res/drawable/textlines.xml并指定这是一个TextView的背景属性。

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

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

向下一面是,你必须指定一个不透明的背景颜色,透明胶片将无法正常工作。 (至少我认为他们没有,但我错了)。在上面的例子中,你可以看到,第一个形状#FFdddddd的纯色被复制到第二个形状描边颜色。

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 #FFdddddd is copied in the 2nd shapes stroke colour.

这篇关于有一个简单的方法来添加边框的一款Android观的顶部和底部?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆