如何从 Xml 文件动态设置 TextView 的背景
我有一个如下所示的 xml 文件,我将用它来设置 Textview
的背景:
row.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient android:endColor="#CCCCCC" android:startColor="#CCCCCC"
android:angle="270" />
<stroke android:width="1dp" android:color="#999999" />
<corners android:bottomRightRadius="0dp"
android:bottomLeftRadius="0dp" android:topLeftRadius="0dp"
android:topRightRadius="0dp" /></shape>
上面的 Xml 我将在 main.xml 中设置为 TextView 的背景,如下所示:
main.xml
<TextView
android:id="@+id/rowtext3"
android:text="Availablity"
android:layout_height="25px"
android:layout_width="60px"
android:textSize="10px"
android:textStyle="bold"
android:textColor="@color/black"
android:gravity="center"
android:background="@drawable/row"
/>
但我希望通过代码而不是 Xml 来完成此操作。我已经完成了在 Xml 中完成的所有操作,例如通过代码动态设置字体、宽度、高度、字体,但无法设置我在中提到的背景.xml 文件。我们如何将 Xml 文件的内容设置为 textview 的背景,类似于我们在 main.xml 中将背景设置为 XML 的方式。
在代码中我这样做了:
t1=new TextView(this); <br>
t1.setText(ed1.getText()); <br>
t1.setHeight(25); <br>
t1.setWidth(60); <br>
t1.setTextSize(10); <br>
但是我没有找到如何设置背景即如何将XML内容设置为背景?
任何人都可以帮我解决这个问题吗?
提前致谢,
I have an xml file like below which I will use to set background for Textview
:
row.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient android:endColor="#CCCCCC" android:startColor="#CCCCCC"
android:angle="270" />
<stroke android:width="1dp" android:color="#999999" />
<corners android:bottomRightRadius="0dp"
android:bottomLeftRadius="0dp" android:topLeftRadius="0dp"
android:topRightRadius="0dp" /></shape>
The above Xml I will set as background for TextView in main.xml as below:
main.xml
<TextView
android:id="@+id/rowtext3"
android:text="Availablity"
android:layout_height="25px"
android:layout_width="60px"
android:textSize="10px"
android:textStyle="bold"
android:textColor="@color/black"
android:gravity="center"
android:background="@drawable/row"
/>
But I want this to do from code rather than Xml.I have done everything that I have done in Xml like font, width, Height, font dynamically through code, but not able to set Background that I mentioned in Xml file. How can we set content of Xml file as background to textview similar to how we set background as XML in main.xml.
In the code I have done like this:
t1=new TextView(this); <br>
t1.setText(ed1.getText()); <br>
t1.setHeight(25); <br>
t1.setWidth(60); <br>
t1.setTextSize(10); <br>
But I didn't find how to set background i.e. how to set XML content as background?
Can any one help me in sorting out this issue?
Thanks In Advance,
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为您正在寻找的方法是
setBackgroundDrawable(Drawable d)
。这将使用给定的 Drawable 设置背景。所以它看起来像这样:
I think the method you're looking for is
setBackgroundDrawable(Drawable d)
.This will set the background using the given Drawable. So it would look something like this:
如果我理解正确的话,
findViewById(int id )
来自 Activity 类就是您要寻找的内容。检索视图后,您可以使用setBackgroundResource(int id)
设置背景。参数id可以在生成的R文件中找到,例如findViewById(R.drawable.row)
。If I understand you correctly,
findViewById(int id)
from the Activity class is what you're looking for. When you have retrieved the view, you can set the background usingsetBackgroundResource(int id)
. The parameter id can be found in your generated R-file, e.g.findViewById(R.drawable.row)
.