为 Android 中的筹码设置边框颜色
Set border color for a chip in Android
在我的 android 应用程序中,我有一个看起来像这样的简单芯片。
有什么方法可以设置边框的颜色使它变成这样吗?
更新:
我尝试添加形状,但在展开布局时出现异常
<android.support.design.chip.Chip
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/chip_with_border"
android:text="my chip" />
drawable/chip_with_border.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle">
<solid android:color="#FFFFFF"/>
<corners android:radius="30dp"/>
<stroke android:width="1dp" android:color="#DDDDDD"/>
</shape>
</item>
</selector>
这会导致异常
android.view.InflateException: Binary XML file line #32: Error inflating class android.support.design.chip.Chip
1- 在 drawable 文件夹中创建 shape.xml
并将此代码放入其中
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle">
<solid android:color="#FFFFFF"/>
<corners android:radius="30dp"/>
<stroke android:width="1dp" android:color="#DDDDDD"/>
</shape>
</item>
</selector>
2- 然后在 View
中设置 android:background="@drawable/shape"
属性
我自己找到了答案。我需要将 chipStrokeColor
和 chipStrokeWidth
属性添加到我的 chip
<android.support.design.chip.Chip
android:id="@+id/chip"
style="@style/Widget.MaterialComponents.Chip.Filter"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:chipStrokeColor="#F0F"
app:chipStrokeWidth="1dp"
android:text="my chip"
app:checkedIcon="@drawable/ic_done_green"
app:chipBackgroundColor="#FFF" />
通过 Material Components Library and the Chip
组件,您可以使用自定义样式:
<com.google.android.material.chip.Chip
style="@style/Colors_Widget.MaterialComponents.Chip.Choice"
..>
或 xml 布局中的 app:chipStrokeColor
、app:chipStrokeWidth
、app:chipBackgroundColor
、android:textColor
属性:
<com.google.android.material.chip.Chip
app:chipBackgroundColor="@color/chip_background_color_selector"
app:chipStrokeColor="@color/color_choice_chip_strokecolor_selector"
app:chipStrokeWidth="1dp"
android:textColor="@color/color_choice_chip_text_color"
..>
采用这种风格:
<style name="Colors_Widget.MaterialComponents.Chip.Choice" parent="Widget.MaterialComponents.Chip.Choice">
<item name="chipBackgroundColor">@color/chip_background_color_selector</item>
<item name="chipStrokeColor">@color/color_choice_chip_strokecolor_selector</item>
<item name="chipStrokeWidth">1dp</item>
<item name="android:textColor">@color/color_choice_chip_text_color</item>
<item name="checkedIconVisible">true</item>
</style>
对于边框颜色,您可以使用选择器来管理不同的状态。
类似于:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="@color/primaryLightColor" android:state_enabled="true" android:state_selected="true"/>
<item android:color="@color/primaryLightColor" android:state_enabled="true" android:state_checked="true"/>
<!-- 87% opacity. -->
<item android:alpha="0.87" android:color="#C6CCCD" android:state_enabled="true"/>
<!-- 38% of 87% opacity. -->
<item android:alpha="0.33" android:color="#C6CCCD"/>
</selector>
在我的 android 应用程序中,我有一个看起来像这样的简单芯片。
有什么方法可以设置边框的颜色使它变成这样吗?
更新: 我尝试添加形状,但在展开布局时出现异常
<android.support.design.chip.Chip
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/chip_with_border"
android:text="my chip" />
drawable/chip_with_border.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle">
<solid android:color="#FFFFFF"/>
<corners android:radius="30dp"/>
<stroke android:width="1dp" android:color="#DDDDDD"/>
</shape>
</item>
</selector>
这会导致异常
android.view.InflateException: Binary XML file line #32: Error inflating class android.support.design.chip.Chip
1- 在 drawable 文件夹中创建 shape.xml
并将此代码放入其中
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle">
<solid android:color="#FFFFFF"/>
<corners android:radius="30dp"/>
<stroke android:width="1dp" android:color="#DDDDDD"/>
</shape>
</item>
</selector>
2- 然后在 View
android:background="@drawable/shape"
属性
我自己找到了答案。我需要将 chipStrokeColor
和 chipStrokeWidth
属性添加到我的 chip
<android.support.design.chip.Chip
android:id="@+id/chip"
style="@style/Widget.MaterialComponents.Chip.Filter"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:chipStrokeColor="#F0F"
app:chipStrokeWidth="1dp"
android:text="my chip"
app:checkedIcon="@drawable/ic_done_green"
app:chipBackgroundColor="#FFF" />
通过 Material Components Library and the Chip
组件,您可以使用自定义样式:
<com.google.android.material.chip.Chip
style="@style/Colors_Widget.MaterialComponents.Chip.Choice"
..>
或 xml 布局中的 app:chipStrokeColor
、app:chipStrokeWidth
、app:chipBackgroundColor
、android:textColor
属性:
<com.google.android.material.chip.Chip
app:chipBackgroundColor="@color/chip_background_color_selector"
app:chipStrokeColor="@color/color_choice_chip_strokecolor_selector"
app:chipStrokeWidth="1dp"
android:textColor="@color/color_choice_chip_text_color"
..>
采用这种风格:
<style name="Colors_Widget.MaterialComponents.Chip.Choice" parent="Widget.MaterialComponents.Chip.Choice">
<item name="chipBackgroundColor">@color/chip_background_color_selector</item>
<item name="chipStrokeColor">@color/color_choice_chip_strokecolor_selector</item>
<item name="chipStrokeWidth">1dp</item>
<item name="android:textColor">@color/color_choice_chip_text_color</item>
<item name="checkedIconVisible">true</item>
</style>
对于边框颜色,您可以使用选择器来管理不同的状态。
类似于:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="@color/primaryLightColor" android:state_enabled="true" android:state_selected="true"/>
<item android:color="@color/primaryLightColor" android:state_enabled="true" android:state_checked="true"/>
<!-- 87% opacity. -->
<item android:alpha="0.87" android:color="#C6CCCD" android:state_enabled="true"/>
<!-- 38% of 87% opacity. -->
<item android:alpha="0.33" android:color="#C6CCCD"/>
</selector>