2014-12-05 51 views
0

我有一个的LinearLayout背景,以创建一个圆角的外观如此设置为绘制文件:试图setBackgroundColor在适配器,可绘制工作不正常

<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更改为“不同”绘制,以改变背景色:

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> 

我失去了一些东西保存圆角?

+0

它是在listview项目中吗?然后尝试调用'invalidate' – 2014-12-05 08:42:44

+0

可以请你张贴适配器完整的代码? – 2014-12-05 08:58:47

回答

0

以为我会分享我的最终修复 - 当我想的

top.setBackgroundColor(R.drawable.rectangle_topcorner_high); 

变种正确的(工作)代码是:

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

尝试使用:

top.setBackgroundResource(R.drawable.rectangle_topcorner_high); 

setBackgroundColor是颜色资源

+0

setBackgroundResource已弃用 – makapaka 2014-12-05 08:45:06

+0

从哪里获取此信息?我正在看Android的src代码,它并没有说任何有关setBackgroundResource已弃用的事实 – royB 2014-12-05 08:49:11

+0

哦,即时通讯抱歉,它是setBackgroundDrawable,我试过了,发现它被弃用,我现在会尝试这个 – makapaka 2014-12-05 08:54:48