27

我正在用 Chips 做一个列表。我希望可以选择这个芯片,所以,看看https://material.io/develop/android/components/chip/我看到我可以有一个“选择芯片”。

由于我需要动态创建和添加,我必须配置特定的颜色、颜色波纹、...

所以我必须配置的是:

val chip = Chip(context, null, R.style.CustomChipChoice)
            chip.isClickable = true
            chip.isCheckable = true
            chip.isCheckedIconVisible=false
            chip.height = ScreenUtils.dpToPx(40)
            chip.chipCornerRadius = (ScreenUtils.dpToPx(20)).toFloat()
            chip.chipStrokeWidth = (ScreenUtils.dpToPx(2)).toFloat()
            chip.setTextAppearanceResource(R.style.ChipTextStyle)
            return chip

我尝试的R.style.CustomChipChoice是:

CustomChipChoice 风格

<style name="CustomChipChoice" parent="@style/Widget.MaterialComponents.Chip.Choice">
        <item name="chipBackgroundColor">@color/background_color_chip_state_list</item>
        <item name="chipStrokeColor">@color/background_color_chip_state_list</item>
        <item name="rippleColor">@color/topic_social_pressed</item>
</style>

background_color_chip_state_list

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:color="@color/topic_social_selected" android:state_checked="true" />
    <item android:color="@color/topic_social_pressed" android:state_pressed="true" />
    <item android:color="@color/topic_unselected_background" />
</selector>

stroke_color_chip_state_list

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:color="@color/topic_social_pressed" android:state_checked="true"/>
    <item android:color="@color/grey_material2" android:state_checked="false"/>
</selector>

如您所见,我制作了可点击和可检查的芯片(隐藏了我不需要的检查图标)。

但是当我测试它时,颜色没有设置。芯片只是看起来像默认颜色(灰度)

我可以在哪里应用或如何应用这种自定义样式?

PS:

我做了一个快速测试,看看我的 CustomStyle 是否格式错误/等等。

我通过xml添加了一个视图并且工作得很好......

<android.support.design.chip.Chip
                android:id="@+id/test"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                style="@style/CustomChipChoice"
                android:checkable="true"
                android:clickable="true"
                app:checkedIconVisible="false"
                android:text="Chip Test"/>
4

5 回答 5

43

不能使用构造函数val chip = Chip(context, null, R.style.CustomChipChoice),因为第三个参数不是样式,而是主题中的属性 as R.attr.chipStyle。没有作为其他组件的具有 4 个参数的构造函数,因为它扩展
不支持 4 参数构造函数。ChipAppCompatCheckbox

但是,您可以使用不同的东西。
第一个选项

只需使用xml 布局( single_chip_layout.xml) 来定义Chip具有您喜欢的风格的单曲:

<com.google.android.material.chip.Chip
    xmlns:android="http://schemas.android.com/apk/res/android"
    style="@style/CustomChipChoice"
    ...
/>

<style name="CustomChipChoice" parent="@style/Widget.MaterialComponents.Chip.Choice">
...
</style>

然后代替val chip = Chip(context, null, R.style.CustomChipChoice)使用:

val chip = layoutInflater.inflate(R.layout.single_chip_layout, chipGroup, false) as Chip

在java中:

Chip chip =
          (Chip) getLayoutInflater().inflate(R.layout.single_chip_layout, chipGroup, false);

第二个选项

另一种选择是使用该setChipDrawable方法覆盖ChipDrawable内部Chip

  Chip chip = new Chip(this);
  ChipDrawable chipDrawable = ChipDrawable.createFromAttributes(this,
      null,
      0,
      R.style.Widget_MaterialComponents_Chip_Choice);
  chip.setChipDrawable(chipDrawable);
于 2019-10-05T10:13:30.423 回答
12

为了在代码中设置芯片样式,您可以尝试以下操作:

val chip = Chip(context)
val drawable = ChipDrawable.createFromAttributes(context, null, 0, R.style.Widget_MaterialComponents_Chip_Choice)
chip.setChipDrawable(drawable)
于 2019-04-30T17:00:23.187 回答
8

CustomChipChoice不是一种风格,它只是一种风格的参考。因此改变R.style.CustomChipChoice它:R.attr.CustomChipChoice

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

但在此之前,您应该将其添加到项目CustomChipChoice中的values.xml文件中。为了这。如果您的项目没有在目录中values.xml创建它。values

然后CustomChipChoice像这样添加。

值.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <attr name="CustomChipChoice" format="reference" />
</resources>

现在styles.xml像这样添加你的风格。

样式.xml

<style name="AppTheme" parent="Theme.MaterialComponents.Light.NoActionBar">
        .
        .
        <item name="CustomChipChoice">@style/CustomChipChoiceStyle</item>
        .
        .
</style>

现在CustomChipChoiceattr 引用了这种样式,现在您可以在styles.xml文件中创建自定义样式。

样式.xml

<style name="CustomChipChoiceStyle" parent="@style/Widget.MaterialComponents.Chip.Action">
        .
        <item name="checkedIconVisible">false</item>
        <item name="android:focusable">true</item>
        <item name="android:clickable">true</item>
        <item name="chipBackgroundColor">@color/colorWhite</item>
        <item name="chipIcon">@drawable/ic_filter</item>
        <item name="chipIconVisible">true</item>
        <item name="textStartPadding">0dp</item>
        <item name="textEndPadding">0dp</item>
        .
        .
        <item name="android:textAppearance">@style/ChipTextStyleAppearance</item>
</style>

如果你想改变芯片的文字外观。这里是ChipTextStyleAppearance。你可以像这样添加它。

样式.xml

<style name="ChipTextStyleAppearance">
        <item name="android:fontFamily">@font/main_font</item>
        <item name="android:textSize">13dp</item>
        <item name="android:textColor">#ffffff</item>
</style>

不要忘记添加AppThemein或标签androidManifest.xmlapplicationactivity

androidManifest.xml

<application
        .
        .
        android:theme="@style/AppTheme">

<activity
            .
            .
            android:theme="@style/AppTheme" />
于 2019-12-18T10:53:37.610 回答
0

科特林

xml

    <com.google.android.material.chip.ChipGroup
                android:id="@+id/chipGroup"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:gravity="center_vertical"
                android:orientation="horizontal">
</com.google.android.material.chip.ChipGroup>

班级

 data class Parametro(
       var idParametro: Long,
       var nombreParametro: String? )

主要的

        listParametro.forEach { it->
                    val chip = Chip(context)
                    chip.id= it.idParametro.toInt()
                    chip.text= it.nombreParametro
                    chip.isClickable = true
                    chip.isCheckable = true
                    chip.setOnCheckedChangeListener { buttonView, isChecked ->
                        Log.i("checkedChipIds","${buttonView.id} $isChecked")
                    }
                    mBinding.chipGroup.addView(chip)
                }

这个对我有用 :)

于 2021-05-31T20:50:52.797 回答
0

还有另一种非常简单的方法。

样式.xml

<style name="Widget.MyApp.Chip" parent="Widget.MaterialComponents.Chip.Choice">
    <item name="android:textAppearance">@style/TextAppearance.MyApp.Chip</item>
    <item name="chipIconTint">?attr/colorPrimary</item>
</style>

主题.xml

<style name="Theme.MyApp.MyTheme" parent="Base.Theme.MyApp">
    <item name="chipStyle">@style/Widget.MyApp.Chip</item>
</style>

有了这个,所有Theme.MyApp.MyActivity应用了主题的活动中的芯片都将遵循这种自定义样式,无论芯片是通过 xml 还是以编程方式添加的。

于 2021-11-03T06:03:16.867 回答