带有Firestore和布局充气机的芯片组单选

问题描述 投票:0回答:1

如何使ChipGroupradioButton一样工作,可以在更改背景颜色的同时一次选择一项。

我看到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);
                        }
                    });
                }
            }
        });
android
1个回答
0
投票

您可以在布局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>

您所选择的颜色是第一行。

© www.soinside.com 2019 - 2024. All rights reserved.