Android 更改ListView中可绘制实例的背景

Android 更改ListView中可绘制实例的背景,android,xml,listview,drawable,Android,Xml,Listview,Drawable,我有一个可绘制的资源drawable/badge.xml,看起来像这样: <?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval" > <solid android:color="#000" /> </shape> <?xml version

我有一个可绘制的资源
drawable/badge.xml
,看起来像这样:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval" >
    <solid android:color="#000" />
</shape>
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="18dp"
    android:layout_height="18dp" >

    <View
        android:id="@+id/badge"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/badge" />

    <TextView
        android:id="@+id/badge_text"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center_vertical|center_horizontal"
        android:textColor="#fff"
        android:textSize="12sp"
        android:textStyle="bold" />

</RelativeLayout>
在BaseExpandableListAdapters
getChildView
方法中,有以下代码:

...
LinearLayout badgeContainer = (LinearLayout) convertView.findViewById(R.id.badge_container);
for (String property : properties) {

    RelativeLayout badge = (RelativeLayout) inflater.inflate(R.layout.badge, badgeContainer, false);

    badge.setLayoutParams(new LinearLayout.LayoutParams(50, 50));

    ((TextView) badge.findViewById(R.id.badge_text)).setText(course.property);
    badge.findViewById(R.id.badge).getBackground().setColorFilter(getColorResourceByProperty(property), PorterDuff.Mode.DST);

    badgeContainer.addView(badge);
}
...

所以我要做的是改变每个
属性的可绘制徽章的背景色,但是这段代码仍然允许所有徽章都有黑色背景。我做错了什么?我还尝试了许多其他PorterDuff模式,但都没有结果。

在多做了一些尝试后,我让它像这样工作:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval" >
    <solid android:color="#000" />
</shape>
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="18dp"
    android:layout_height="18dp" >

    <View
        android:id="@+id/badge"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/badge" />

    <TextView
        android:id="@+id/badge_text"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center_vertical|center_horizontal"
        android:textColor="#fff"
        android:textSize="12sp"
        android:textStyle="bold" />

</RelativeLayout>
drawable/circle.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval" >

    <solid android:color="#fff" />

</shape>
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/badge_text"
    android:layout_width="16dp"
    android:layout_height="16dp"
    android:gravity="center_vertical|center_horizontal"
    android:background="@drawable/circle"
    android:textColor="@color/white"
    android:textSize="12sp"
    android:layout_marginLeft="1dp"
    android:textStyle="bold" />
在适配器内部:

LinearLayout badgeContainer = (LinearLayout) convertView.findViewById(R.id.badge_container);
for (int i = 0; i < course.properties.length; i++) {

    TextView badge = (TextView) inflater.inflate(R.layout.badge, badgeContainer, false);
    badge.setText(course.properties[i]);
    int colorID = context.getResources().getIdentifier("property_" + course.properties[i], "color", context.getPackageName());
    badge.getBackground().setColorFilter(context.getResources().getColor(colorID), Mode.MULTIPLY);

    badgeContainer.addView(badge);
}
LinearLayout-badgeContainer=(LinearLayout)convertView.findviewbyd(R.id.badge\u-container);
对于(int i=0;i