Change Chip Widget style programmatically not working - Android

后端 未结 3 1727
说谎
说谎 2020-12-02 01:53

I\'m doing a list with Chips. I want this chips can be selected, so, taking a look to https://material.io/develop/android/components/chip/ I see I can have a \"Choice Chip\"

3条回答
  •  旧巷少年郎
    2020-12-02 02:27

    the CustomChipChoice is not a style it is just a reference to a style. therefore change R.style.CustomChipChoice to it : R.attr.CustomChipChoice

    val newChip = Chip(context, null, R.attr.CustomChipChoice)
    

    but before it you should add this CustomChipChoicein values.xml file in your project. for this. if your project does not have the values.xml create it in values directory.

    then add CustomChipChoice like this.

    values.xml

    
    
        
    
    

    now in styles.xml add your style like this.

    styles.xml

    
    

    now that CustomChipChoice attr references to this style and now you can create your custom style in styles.xml file.

    styles.xml

    
    

    if you want to change text appearance of chip. here is ChipTextStyleAppearance. you can add it like this.

    styles.xml

    
    

    dont forget to add the AppTheme in androidManifest.xml on application or activity tags.

    androidManifest.xml

    
    
    
    

提交回复
热议问题