如何使ChipGroup
像radioButton
一样工作,可以在更改背景颜色的同时一次选择一项。
我看到this link是这样的东西,但因为使用了layoutInflater
来显示我的筹码项目,所以对我没有帮助。
firebaseFirestore.collection("Categories").addSnapshotListener((queryDocumentSnapshots, e) -> {
for (DocumentChange doc: queryDocumentSnapshots.getDocumentChanges()){
if (doc.getType() == DocumentChange.Type.ADDED){
Categories categories = doc.getDocument().toObject(Categories.class);
post_list.add(categories);
Chip chip = (Chip) getLayoutInflater().inflate(R.layout.chip_item_layout, chipGroup, false);
chip.setText(categories.getTitle());
chipGroup.addView(chip);
chip.setOnCheckedChangeListener((buttonView, isChecked) -> {
if(isChecked){
chip.setChipBackgroundColorResource(R.color.customOrange);
}
});
}
}
});
您可以在布局app:chipBackgroundColor
中使用chip_item_layout.xml
属性。
类似:
<com.google.android.material.chip.Chip
app:chipBackgroundColor="@color/chip_background_color"
..>
chip_background_color
是一个选择器,您可以在其中定义自己喜欢的颜色。这是默认值:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- 24% opacity -->
<item android:alpha="0.24" android:color="?attr/colorPrimary" android:state_enabled="true" android:state_selected="true"/>
<item android:alpha="0.24" android:color="?attr/colorPrimary" android:state_enabled="true" android:state_checked="true"/>
<!-- 12% of 87% opacity -->
<item android:alpha="0.10" android:color="?attr/colorOnSurface" android:state_enabled="true"/>
<item android:alpha="0.12" android:color="?attr/colorOnSurface"/>
</selector>
您所选择的颜色是第一行。