2013-11-09 68 views
0

我嘗試將背景應用於listview項目中的textview。 我應用背景的適配器:listitem中元素的背景

public View getView(int position, View convertView, ViewGroup parent) { 
    ... 
    viewHolder.textview.setBackground(background); 
    ... 
} 

此外,佈局寬度設置好的這樣的:

android:layout_width="wrap_content" 

這裏是背景的XML:

<?xml version="1.0" encoding="utf-8"?> 
    <shape xmlns:android="http://schemas.android.com/apk/res/android" > 
     <solid android:color="#ff71b10c" /> 
     <corners android:radius="5dp" /> 
    </shape> 

然而,背景不適合textview的寬度,我嘗試了幾個技巧,但沒有任何工作。

您可以在這裏看到的錯誤:

與BUG:http://imageshack.us/a/img811/6742/3rx9.png

沒有錯誤:http://imageshack.us/a/img14/9075/wfk8.png

我不知道那是什麼。任何洞察力將不勝感激。

謝謝。

+0

textview是否將layout_width設置爲match_parent? –

+0

抱歉,我還沒有指定,我編輯了我的問題。 – juzamn

+1

您是否嘗試過撥打setbackgroundresource並將其傳遞給ID?或者只是在xml中設置背景並查看結果。您還可以將寬度設置爲match_parent與邊距和填充。 – meh

回答

0

我不爲什麼,但用setBackgroundResource背景適合寬度正確。

viewHolder.textview.setBackgroundResource(R.drawable.background_id);