[英]Set border color for a chip in Android
In my android app I have a simple chip that looks like this.在我的 android 应用程序中,我有一个看起来像这样的简单芯片。
Is there any way to set the color of the border to make it like this?有没有办法设置边框的颜色,使它像这样?
UPDATE : I tried to do add the shape but there's an exception during inflating the layout更新:我尝试添加形状,但在膨胀布局期间出现异常
<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>
This causes the exception这导致异常
android.view.InflateException: Binary XML file line #32: Error inflating class android.support.design.chip.Chip
android.view.InflateException:二进制 XML 文件第 32 行:错误膨胀类 android.support.design.chip.Chip
I found the answer myself.我自己找到了答案。 I need to add
chipStrokeColor
and chipStrokeWidth
attributes to my chip
我需要为我的
chip
添加chipStrokeColor
和chipStrokeWidth
属性
<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" />
With the Material Components Library and the Chip
component you can use a custom style:通过材料组件库和
Chip
组件,您可以使用自定义样式:
<com.google.android.material.chip.Chip
style="@style/Colors_Widget.MaterialComponents.Chip.Choice"
..>
or the app:chipStrokeColor
, app:chipStrokeWidth
, app:chipBackgroundColor
, android:textColor
attributes in the xml layout:或 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"
..>
With this style:使用这种风格:
<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>
For the border color you can use a selector to manage the different states.对于边框颜色,您可以使用选择器来管理不同的状态。
Something like:就像是:
<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>
1- Create shape.xml
in drawable folder and put this code inside it 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- then set android:background="@drawable/shape"
attribute in your View
2-然后在您的
View
设置android:background="@drawable/shape"
属性
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.