我在用薯片做一份名单。我想这个芯片可以选择,所以,看看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
<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>正如您所看到的,我使芯片,可点击和可检查(隐藏的检查图标,我不需要)。
但是当我测试它的时候,颜色没有被设定。芯片看起来只是默认的颜色(灰色的比例)
我可以在哪里申请,或者如何申请,这种定制风格?
P.S:
我做了一个快速测试,看看我的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"/>发布于 2019-10-05 10:13:30
不能使用构造函数val chip = Chip(context, null, R.style.CustomChipChoice),因为第3参数不是样式,而是主题中的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);发布于 2019-04-30 17:00:23
为了在代码中设置芯片样式,您可以尝试以下操作:
val chip = Chip(context)
val drawable = ChipDrawable.createFromAttributes(context, null, 0, R.style.Widget_MaterialComponents_Chip_Choice)
chip.setChipDrawable(drawable)发布于 2019-12-18 10:53:37
CustomChipChoice不是一种样式,它只是对一种样式的引用。因此,将R.style.CustomChipChoice更改为:R.attr.CustomChipChoice
val newChip = Chip(context, null, R.attr.CustomChipChoice)但在此之前,您应该将此CustomChipChoice添加到项目的values.xml文件中。为了这个。如果您的项目没有values.xml,请在values目录中创建它。
然后像这样添加CustomChipChoice。
values.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<attr name="CustomChipChoice" format="reference" />
</resources>现在在styles.xml中添加这样的样式。
styles.xml
<style name="AppTheme" parent="Theme.MaterialComponents.Light.NoActionBar">
.
.
<item name="CustomChipChoice">@style/CustomChipChoiceStyle</item>
.
.
</style>既然CustomChipChoice引用了这种样式,现在就可以在styles.xml文件中创建自定义样式了。
styles.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。你可以这样加进去。
styles.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>不要忘记在AppTheme或activity标签上添加androidManifest.xml中的application。
androidManifest.xml
<application
.
.
android:theme="@style/AppTheme">
<activity
.
.
android:theme="@style/AppTheme" />https://stackoverflow.com/questions/53557863
复制相似问题