2014-12-05 48 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