试图将setBackgroundColor设置为适配器中的drawable不能按预期工作

时间:2014-12-05 08:38:33

标签: android android-layout

我将线性布局背景设置为可绘制文件,以便创建圆角视图:

<LinearLayout
    android:id="@+id/layout_top"
    android:layout_width="fill_parent"
    android:layout_height="0dp"
    android:layout_weight="1"
    android:background="@drawable/rectangle_topcorner">

     <TextView android:layout_width="fill_parent"
        android:id="@+id/textView1"
        android:layout_height="wrap_content"
        android:text="Impact"
        android:layout_gravity="center"
        android:textColor="@android:color/white"
        android:layout_marginLeft="10dip"  />
</LinearLayout>

在我的适配器中,在我给布局项目膨胀之后,我正在尝试将backgroundColor更改为“不同的&#39; drawable,为了改变背景颜色:

LinearLayout top = (LinearLayout) view.findViewById(R.id.layout_top);
top.setBackgroundColor(R.drawable.rectangle_topcorner_high);

问题是,在这样做之后,矩形失去了它的圆角外观,它只是一个普通的老方块。

2个抽奖: 的 rectangle_topcorner

<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle"
    android:id="@+id/background_shape" >
<corners android:topLeftRadius="30dp"
    android:topRightRadius="30dp" />
<solid android:color="#005577"/>  
</shape>

rectangle_topcorner_high

<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle"
    android:id="@+id/background_shape" >
<corners android:topLeftRadius="30dp"
    android:topRightRadius="30dp" />
<solid android:color="#83162D"/>  
</shape>

我错过了保留圆角的东西?

2 个答案:

答案 0 :(得分:0)

尝试使用:

top.setBackgroundResource(R.drawable.rectangle_topcorner_high);

setBackgroundColor用于颜色资源

答案 1 :(得分:0)

以为我会分享我最终的修复 - 当我尝试变种

top.setBackgroundColor(R.drawable.rectangle_topcorner_high);

正确(正常)代码是:

top.setBackground(context.getResources().getDrawable(R.drawable.rectangle_topcorner_high));