以编程方式更改芯片小部件样式不起作用 - Android [英] Change Chip Widget style programmatically not working - Android

查看:34
本文介绍了以编程方式更改芯片小部件样式不起作用 - Android的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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".

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

As I need to create and add dynamically I have to configure with specific colors, color ripplem, ...

所以我必须配置它:

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>

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

As you can see, I make the chip, clickable and checkable (hiding the check icon I don't need).

但是当我测试它时,颜色没有设置.芯片看起来只是默认颜色(灰色的比例)

But when I test it, the colors are not set. The chips just look as default colors (grey's scale)

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

Where can I apply or how, this custom style?

附注:

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

I have done a fast test, to see if my CustomStyle was malformed/etc..

我通过 xml 添加了一个视图并且运行良好...

I added a view via xml and worked perfectly...

<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"/>

推荐答案

不能使用构造函数 val chip = Chip(context, null, R.style.CustomChipChoice) 因为 3rd 参数不是样式,而是主题中的属性,如 R.attr.chipStyle.
Chip 没有像其他组件那样具有 4 个参数的构造函数,因为它扩展了不支持 4 个参数构造函数的 AppCompatCheckbox.

You can't use the constructor val chip = Chip(context, null, R.style.CustomChipChoice) because the 3rd parameter isn't the style but the attribute in the theme as R.attr.chipStyle.
The Chip hasn't a constructor with 4 parameters as other components because it extends AppCompatCheckbox which does not support a 4 parameter constructor.

但是你可以使用不同的东西.
第一个选项:

However you can use something different.
1st option:

只需使用 xml 布局 (single_chip_layout.xml) 来定义具有您最喜欢的样式的单个 Chip:

Just use a xml layout (single_chip_layout.xml) to define the single Chip with your favorite style:

<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方法来覆盖Chip中的ChipDrawable:

Another option is to use the setChipDrawable method to override the ChipDrawable inside the Chip:

  Chip chip = new Chip(this);
  ChipDrawable chipDrawable = ChipDrawable.createFromAttributes(this,
      null,
      0,
      R.style.Widget_MaterialComponents_Chip_Choice);
  chip.setChipDrawable(chipDrawable);

这篇关于以编程方式更改芯片小部件样式不起作用 - Android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆