以编程方式从Android中的drawable创建textview背景 [英] Programmatically create textview background from drawable in Android
本文介绍了以编程方式从Android中的drawable创建textview背景的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我必须在 Android TextView 上以编程方式设置背景
I have to set the background programmatically on Android TextView
我使用下面的代码.它不起作用,而且它给了我 nullpointerexception 错误.
I have using below code. it's not working and also its gave me nullpointerexception error.
best_deals = (TextView) findViewById(R.id.bestdeals);
best_deals.setBackground(getResources().getDrawable(
R.drawable.headerradius));
但我必须把这些
best_deals.setTextColor(Color.parseColor("#be2351"));
表示它正在工作
上面的代码有什么问题?
what's wrong in above code?
这是我的 header_redius.xml:
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" android:padding="10dp">
<solid android:color="#000000"/>
<corners
android:radius="15dp"
/>
<gradient
android:startColor="#434343"
android:centerColor="#434343"
android:endColor="#434343"
android:angle="270"
android:type="linear"
/>
<padding android:left="10dp"
android:top="0dp"
android:right="10dp"
android:bottom="0dp"/>
<stroke
android:width="2dp"
android:color="#000000" />
</shape>
推荐答案
你需要使用 setBackgroundResource()
方法.
例如:
best_deals.setBackgroundResource(R.drawable.headerradius);
这篇关于以编程方式从Android中的drawable创建textview背景的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文