![](/img/trans.png)
[英]How to set text color in android.support.design.chip.Chip?
[英]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:二進制 XML 文件第 32 行:錯誤膨脹類 android.support.design.chip.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" />
<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>
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"
屬性
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.