Shu*_*udy 7 android kotlin android-chips material-components material-components-android
我正在用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
Run Code Online (Sandbox Code Playgroud)
我尝试的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>
Run Code Online (Sandbox Code Playgroud)
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>
Run Code Online (Sandbox Code Playgroud)
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>
Run Code Online (Sandbox Code Playgroud)
如您所见,我将芯片制作为可点击且可检查的(隐藏不需要的选中图标)。
但是当我测试它时,没有设置颜色。芯片看起来只是默认的颜色(灰度)
这种自定义样式可以在哪里应用或如何应用?
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"/>
Run Code Online (Sandbox Code Playgroud)
Gab*_*tti 39
您不能使用构造函数,val chip = Chip(context, null, R.style.CustomChipChoice)因为第三个参数不是样式,而是主题中的属性 as R.attr.chipStyle。
该Chip具有不与4个参数作为其它成分,因为它延伸的构造AppCompatCheckbox不支持4参数构造。
但是,您可以使用不同的东西。
第一个选项:
只需使用xml 布局( single_chip_layout.xml) 来定义Chip具有您喜欢的样式的单曲:
<com.google.android.material.chip.Chip
xmlns:android="http://schemas.android.com/apk/res/android"
style="@style/CustomChipChoice"
...
/>
Run Code Online (Sandbox Code Playgroud)
和
<style name="CustomChipChoice" parent="@style/Widget.MaterialComponents.Chip.Choice">
...
</style>
Run Code Online (Sandbox Code Playgroud)
然后而不是val chip = Chip(context, null, R.style.CustomChipChoice)使用:
val chip = layoutInflater.inflate(R.layout.single_chip_layout, chipGroup, false) as Chip
Run Code Online (Sandbox Code Playgroud)
在Java中:
Chip chip =
(Chip) getLayoutInflater().inflate(R.layout.single_chip_layout, chipGroup, false);
Run Code Online (Sandbox Code Playgroud)
第二个选项:
另一种选择是使用该setChipDrawable方法来覆盖ChipDrawable内部Chip:
Chip chip = new Chip(this);
ChipDrawable chipDrawable = ChipDrawable.createFromAttributes(this,
null,
0,
R.style.Widget_MaterialComponents_Chip_Choice);
chip.setChipDrawable(chipDrawable);
Run Code Online (Sandbox Code Playgroud)
小智 12
为了在代码中设置芯片样式,您可以尝试以下操作:
val chip = Chip(context)
val drawable = ChipDrawable.createFromAttributes(context, null, 0, R.style.Widget_MaterialComponents_Chip_Choice)
chip.setChipDrawable(drawable)
Run Code Online (Sandbox Code Playgroud)
这CustomChipChoice不是一种风格,它只是一种风格的参考。因此更改R.style.CustomChipChoice为:R.attr.CustomChipChoice
val newChip = Chip(context, null, R.attr.CustomChipChoice)
Run Code Online (Sandbox Code Playgroud)
但在此之前它要添加这个CustomChipChoice在values.xml您的项目文件。为了这。如果您的项目没有values.xml在values目录中创建它。
然后CustomChipChoice像这样添加。
值.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<attr name="CustomChipChoice" format="reference" />
</resources>
Run Code Online (Sandbox Code Playgroud)
现在styles.xml像这样添加你的风格。
样式文件
<style name="AppTheme" parent="Theme.MaterialComponents.Light.NoActionBar">
.
.
<item name="CustomChipChoice">@style/CustomChipChoiceStyle</item>
.
.
</style>
Run Code Online (Sandbox Code Playgroud)
现在CustomChipChoiceattr 引用了这种样式,现在您可以在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>
Run Code Online (Sandbox Code Playgroud)
如果您想更改芯片的文本外观。这里是ChipTextStyleAppearance。你可以这样添加。
样式文件
<style name="ChipTextStyleAppearance">
<item name="android:fontFamily">@font/main_font</item>
<item name="android:textSize">13dp</item>
<item name="android:textColor">#ffffff</item>
</style>
Run Code Online (Sandbox Code Playgroud)
不要忘了加AppTheme在androidManifest.xml上application或activity标签。
androidManifest.xml
<application
.
.
android:theme="@style/AppTheme">
<activity
.
.
android:theme="@style/AppTheme" />
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1262 次 |
| 最近记录: |