有没有一种简单的方法可以在 Android 视图的顶部和底部添加边框? [英] Is there an easy way to add a border to the top and bottom of an Android View?
问题描述
我有一个 TextView,我想在它的顶部和底部边框上添加一个黑色边框.我尝试将 android:drawableTop
和 android: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 中轻松地为 View(特别是 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 drawable,例如/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屋!