gpt4 book ai didi

android - 以编程方式更改芯片小部件样式不起作用 - Android

转载 作者:IT老高 更新时间:2023-10-28 13:45:56 27 4
gpt4 key购买 nike

我正在用 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 的尝试是:

自定义芯片选择样式

<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 添加了一个 View 并且运行良好...

<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

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

只需使用 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 方法覆盖 Chip 内的 ChipDrawable:

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

关于android - 以编程方式更改芯片小部件样式不起作用 - Android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53557863/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com