2012-10-23 66 views
0

如果我有一个绘制形状:如何通过Java在Android中使用可绘制的形状?

<?xml version="1.0" encoding="UTF-8"?> 
<shape xmlns:android="http://schemas.android.com/apk/res/android" 
android:shape="rectangle"> 
<stroke android:width="2dp" android:color="#FFFFFFFF" /> 
<gradient android:startColor="#DD000000" android:endColor="#DD2ECCFA" 
     android:angle="225"/> 

<corners android:bottomRightRadius="7dp" android:bottomLeftRadius="7dp" 
android:topLeftRadius="7dp" android:topRightRadius="7dp"/> 
</shape> 

我怎么会去使用这个为背景的一个TextView?我尝试了类似于下面的内容,但没有奏效。

TextView jObjTv = new TextView(getActivity()); 
jObjTv.setBackgroundDrawable(findViewById(R.drawable.sample_box)); 

回答

3

您发布的代码不起作用,因为drawable不是视图,也不是布局的一部分。

如果您要使用资源,则需要致电textView.setBackgroundResource(R.drawable.sample_box)。如果您想使用Drawable,则需要使用context.getResources().getDrawable(R.drawable.sample_box)创建Drawable。