【问题标题】:Set com.google.android.material.chip.Chip selected color设置 com.google.android.material.chip.Chip 选择颜色
【发布时间】:2018-12-07 21:44:33
【问题描述】:

如何设置选定的 com.google.android.material.chip.Chip 颜色?我不希望它成为默认的灰色。这是一个单一的选择芯片组。

原始文档here

<com.google.android.material.chip.ChipGroup
    android:id="@+id/chipgroup"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_marginStart="16dp"
    android:layout_marginTop="16dp"
    android:layout_marginEnd="16dp"
    app:checkedChip="@+id/chip_program"
    app:chipSpacingHorizontal="32dp"
    app:chipSpacingVertical="8dp"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/detailText"
    app:singleSelection="true">

    <com.google.android.material.chip.Chip
        android:id="@+id/chip_program"
        style="@style/Widget.MaterialComponents.Chip.Choice"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Program"
        app:chipEndPadding="16dp"
        app:chipStartPadding="16dp" />

    <com.google.android.material.chip.Chip
        android:id="@+id/chip_normal"
        style="@style/Widget.MaterialComponents.Chip.Choice"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/program_normal"
        app:chipEndPadding="16dp"
        app:chipStartPadding="16dp" />
</com.google.android.material.chip.ChipGroup>

【问题讨论】:

    标签: android material-design material-components-android android-chips


    【解决方案1】:

    只需设置一个属性app:chipBackgroundColor 并将颜色状态列表传递给它:

    <android.support.design.chip.Chip
        android:id="@+id/test"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:checkable="true"
        android:clickable="true"
        android:focusable="true"
        app:chipBackgroundColor="@color/bg_chip_state_list"
        app:chipText="Test" />
    

    bg_chip_state_list 看起来像这样:

    <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:color="@color/colorSecondaryLight" android:state_checked="true" />
        <item android:color="@color/colorPrimaryDark" />
    </selector>
    

    但是我还必须将 android:clickable 设置为 true 才能完成这项工作

    【讨论】:

    • 它工作正常,你是怎么发现的?这是医生吗? developer.android.com/guide/topics/resources/…
    • 最好提到文件位置是 res/color/bg_chip_state_list.xml
    • 我不需要将 clickable 设置为 true。
    • 现在几乎是同一个问题,如何对文本颜色做同样的事情?
    • 我必须使用 state_selected 而不是 state_checked 才能使其工作。不过我使用的是Choice Chip 风格。
    【解决方案2】:

    使用ColorStateList 是正确的方法。 我唯一要添加的是使用自定义定义的样式,阅读起来更清晰,特别是如果您想自定义一堆属性。

    除其他外,一种应用于所有视图的通用样式允许您在一个位置进行更改,立即应用于所有视图

    styles.xml

    <style name="CustomChipChoice" parent="@style/Widget.MaterialComponents.Chip.Choice">
            <item name="chipBackgroundColor">@color/background_color_chip_state_list</item>
            <item name="android:textColor">@color/text_color_chip_state_list</item>
    </style>
    

    text_color_chip_state_list.xml

    <selector xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:state_checked="true"
            android:color="@color/color_checked" />
        <item android:state_checked="false"
            android:color="@color/color_unchecked" />
    </selector>
    

    background_color_chip_state_list.xml

    <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:color="@color/color1" android:state_checked="true" />
        <item android:color="@color/color2" />
    </selector>
    

    之后,您只需为所有 Chip 视图应用您的自定义样式,就像这样。

    <android.support.design.chip.Chip
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        style="@style/CustomChipChoice"
        android:checkable="true"
        android:clickable="true"
        android:focusable="true"
        android:text="Chip text" />
    

    【讨论】:

    • 如何以编程方式设置样式?我无法正确设置它
    • 抱歉让您久等了...我有答案,但不确定这是否是以编程方式将样式应用于视图的最佳方式。 1。值/attrs.xml &lt;resources&gt; &lt;attr name="CustomChipChoiceStyle" format="reference" /&gt; &lt;/resources&gt; 2.样式.xml &lt;!-- Base application theme. --&gt; &lt;style name="AppTheme" parent="Base.Theme.MaterialComponents.Light"&gt; ... &lt;item name="CustomChipChoiceStyle"&gt;@style/CustomChipChoice&lt;/item&gt; &lt;/style&gt; 3.在代码中 val chip = Chip(requireContext(), null, R.attr.CustomChipChoiceStyle)
    • 那么 checkedIconVisible 选中的图标仍然是黑色的怎么改变呢?
    • app:checkedIconVisible="true" app:checkedIcon="@drawable/your_custom_icon"
    【解决方案3】:

    要更改Chip 中的颜色,您可以使用自定义样式:

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

    采用这种风格:

     <style name="My_Widget.MaterialComponents.Chip.Choice" parent="Widget.MaterialComponents.Chip.Choice">
        <!-- Chip background color selector -->
        <item name="chipBackgroundColor">@color/my_choice_chip_background_color</item>
        <!-- Border color -->
        <item name="chipStrokeColor">@color/primaryDarkColor</item>
    
        <!-- Chip text color selector -->
        <item name="android:textColor">@color/mtrl_choice_chip_text_color</item>
        <!-- Chip close icon color selector -->
        <item name="closeIconTint">@color/mtrl_chip_close_icon_tint</item>
      </style>
    

    对于 chipBackgroundColor,您可以使用如下选择器:

    <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
      <!-- 24% opacity -->
      <item android:alpha="0.24" android:color="@color/custom" android:state_enabled="true" android:state_selected="true"/>
      <item android:alpha="0.24" android:color="@color/secondaryDarkColor" android:state_enabled="true" android:state_checked="true"/>
      <!-- 12% of 87% opacity -->
      <item android:alpha="0.10" android:color="@color/primaryLightColor" android:state_enabled="true"/>
      <item android:alpha="0.12" android:color="@color/colorPrimary"/>
    
    </selector>
    

    对于文本颜色,您可以使用如下内容:

    <selector xmlns:android="http://schemas.android.com/apk/res/android">
    
      <item android:color="@color/colorAccent" android:state_enabled="true" android:state_selected="true"/>
      <item android:color="?attr/colorPrimary" android:state_enabled="true" android:state_checked="true"/>
      <!-- 87% opacity. -->
      <item android:alpha="0.87" android:color="?attr/colorOnSurface" android:state_enabled="true"/>
      <!-- 38% of 87% opacity. -->
      <item android:alpha="0.33" android:color="?attr/colorOnSurface"/>
    
    </selector>
    

    正常/选中状态的结果:

    【讨论】:

      【解决方案4】:

      对于那些使用alpha-05 的人,我发现state_checked 在可过滤(parent="Widget.MaterialComponents.Chip.Filter")芯片上被忽略了。相反,你需要state_selected:

      <?xml version="1.0" encoding="utf-8"?>
      <selector xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:color="@color/apricot" android:state_selected="true"/>
        <item android:color="@color/apricotSubtle"/>
      </selector>
      

      【讨论】:

      • 那么 checkedIconVisible 选中的图标仍然是黑色的怎么改变呢?
      【解决方案5】:

      正如其他人提到的,您需要将芯片元素的背景颜色属性设置为您定义的 ColorStateList。但我只是想指出一个关于如何做到这一点的重要说明,因为我遇到了让不同状态工作的问题。

      在定义您自己的 ColorStateList(xml 资源)时,您需要确保在 ColorStateList 默认颜色之前设置不同的状态选项!在我弄清楚之前,这让我绊倒了几天,所以我希望这对其他人也有帮助。

      此外,您的芯片需要是可点击和可聚焦的(可检查对我不起作用)所以也将这些属性设置为 true。

      <?xml version="1.0" encoding="utf-8"?>
      <selector xmlns:android="http://schemas.android.com/apk/res/android">
          <item android:state_selected="true"
                android:color="@color/chipColorLight" />
          <item android:color="@color/chipColorDefault"/>
      </selector>
      

      如果您想以编程方式设置不同的 ColorStateOptions,您可以这样做:

      binding.myChip.chipBackgroundColor = resources.getColorStateList(R.color.chip_color_state_list)
      

      【讨论】:

        【解决方案6】:

        以某种方式将android:textColor 更改为styles 对我不起作用。我必须以编程方式更改芯片的文本颜色(因为我也以编程方式创建芯片)。

        val chip = Chip(context)
        // Apply custom MyChipChoice style to the chip
        val drawable = ChipDrawable.createFromAttributes(context!!, null, 0, R.style.MyChipChoice)
        chip.setChipDrawable(drawable)
        // Apply text color to the chip
        val colorStateList = ContextCompat.getColorStateList(context!!, R.color.my_choice_chip_text_color)
        chip.setTextColor(colorStateList)
        

        【讨论】:

          【解决方案7】:

          如果您在代码中创建芯片项,请使用上述状态列表和以下方法(当然是在 Java 中):

          chip.setClickable(true);
          chip.setCheckable(true);
          chip.setChipBackgroundColor(getColorStateList(R.color.chip_background_color));
          chip.setCheckedIconVisible(false);
          

          注意: getColorStateList 要求 build.gradle 脚本中的 minSdkVersion 为 23。

          【讨论】:

            【解决方案8】:

            看看这个...

            <com.google.android.material.chip.ChipGroup
                        android:id="@+id/chipGroupFilter"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:theme="@style/Theme.MaterialComponents.Light.DarkActionBar"
                        app:selectionRequired="true"
                        app:singleLine="true"
                        app:singleSelection="true">
            
                        <com.google.android.material.chip.Chip
                            android:id="@+id/chipAll"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:text="All"
                            android:checkable="true"
                            android:clickable="true"
                            android:focusable="true"
                            app:chipBackgroundColor="@color/bg_chip_state_list"
                            app:checkedIconEnabled="false"
                            android:textColor="@color/whiteBlackSwitchColor"
                            app:chipIcon="@drawable/ic_all"
                            app:chipIconTint="#4D4F55"
                            app:chipIconVisible="true" />
                 </com.google.android.material.chip.ChipGroup>
            

            【讨论】:

              【解决方案9】:

              所以你可以使用setChipBackgroundColor(ColorStateList cl)方法来设置你的芯片的颜色,然后你可以添加一个setOnClickListener(new ...)来切换选择和非选择,如下代码:

              yourchip.setOnClickListener(new View.OnClickListener() {
                  @Override
                  public void onClick(View v) {
                      if (((Chip)v).getChipBackgroundColor().equals(getResources().getColorStateList(R.color.colorPrimaryDark,null))) {
                          ((Chip)v).setChipBackgroundColor(getResources().getColorStateList(R.color.colorPrimary, null));
                      } else {
                          ((Chip) v).setChipBackgroundColor(getResources().getColorStateList(R.color.colorPrimaryDark, null));
                      }
                  }
              });
              
              

              我使用colorPrimaryDark 进行选择,colorPrimary 用于非选择。

              【讨论】:

                猜你喜欢
                • 1970-01-01
                • 2015-03-01
                • 1970-01-01
                • 2019-03-31
                • 2018-01-29
                • 1970-01-01
                • 1970-01-01
                • 2020-11-27
                • 1970-01-01
                相关资源
                最近更新 更多