设置textView的边框和背景颜色 [英] Set border and background color of textView
问题描述
我在 XML 中定义了一个 TextView,我想为其设置背景颜色和边框.我遇到的问题是,在 XML 中我已经使用 android:background
来设置边框资源,所以我不能再次将它用于背景颜色.有人可以指导我正确的方向吗?
I have a TextView defined in XML and i would like to set background color AND border to it.
Problem i have is that in XML i already use android:background
for setting border resource, so i can't use it once again for background color.
Can someone please guide me to right direction?
边框.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" >
<solid android:color="#ffffff" />
<stroke android:width="1dip" android:color="#7F000000"/>
</shape>
文本视图
<TextView
android:id="@+id/editor_title"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:background="@drawable/title_border"
android:padding="5dp"
android:text="@string/editor_title"
android:textAppearance="?android:attr/textAppearanceMedium" />
推荐答案
您应该为此创建一个 XML 可绘制对象,然后可以将其设置为您的单一背景.这就是您想要的(具有不同颜色边框的矩形 - 如果您不想要,请将渐变替换为).
You should create a XML drawable for this, which can then be set as your single background. Here is what you are wanting (a rectangle with a different color border - replace gradient with if you don't want that).
这将进入您的drawable"文件夹:
This will go in your 'drawable' folder:
<?xml version="1.0" encoding="utf-8"?>
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<stroke android:width="3dp" android:color="@color/blue_button_border" />
<gradient
android:startColor="@color/gradient_end"
android:endColor="@color/gradient_start"
android:angle="-90" />
</shape>
这篇关于设置textView的边框和背景颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!