0

如果我有一个可绘制的形状:

<?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 jObjTv = new TextView(getActivity());
jObjTv.setBackgroundDrawable(findViewById(R.drawable.sample_box));
4

1 回答 1

3

您发布的代码不起作用,因为可绘制对象不是视图,也不是布局的一部分。

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

于 2012-10-23T21:30:18.913 回答