为Android中的芯片设置边框颜色

时间:2019-03-22 18:47:56

标签: android styles material android-chips

在我的android应用中,我有一个看起来像这样的简单芯片。

enter image description here

有没有办法设置边框的颜色使其像这样?

enter image description here

更新: 我尝试添加形状,但在布局放大时出现了异常

 <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

3 个答案:

答案 0 :(得分:3)

我自己找到了答案。我需要将chipStrokeColorchipStrokeWidth属性添加到我的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" />

enter image description here

答案 1 :(得分:3)

通过Material Components LibraryChip组件,您可以使用自定义样式:

<com.google.android.material.chip.Chip
    style="@style/Colors_Widget.MaterialComponents.Chip.Choice"
    ..>

或xml布局中的app:chipStrokeColorapp:chipStrokeWidthapp:chipBackgroundColorandroid: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>

enter image description here

答案 2 :(得分:1)

1-在可绘制文件夹中创建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-然后在您的android:background="@drawable/shape"中设置View