当setBackgroundResource时,xml形状drawable不起作用,但是png drawable可以。 我在ListView适配器的getView()中使用它:
RelativeLayout rl = new RelativeLayout(ctx);
rl.setLayoutParams(new ListView.LayoutParams(300, 100));
rl.setBackgroundResource(R.drawable.curve_shap);//no effect
//rl.setBackgroundResource(R.drawable.ic_launcher);//png drawable,works well
return rl;
/res/drawable/curve_shap.xml
(它适用于静态xml布局文件中RelativeLayout的背景)
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<solid android:color="#FFFF0000" />
<stroke
android:width="2dp"
android:color="#ffffffff" />
<corners
android:bottomLeftRadius="20dp"
android:bottomRightRadius="20dp"
android:topLeftRadius="20dp"
android:topRightRadius="20dp" />
</shape>