【问题标题】:MaterialComponents CheckBox, how to keep the icon's original colorsMaterialComponents CheckBox,如何保持图标原来的颜色
【发布时间】:2021-12-05 10:01:25
【问题描述】:

我最近将应用程序的主题从 Theme.AppCompat 更改为 Theme.MaterialComponents,并且需要阻止我的 CheckBox 视图在图标可绘制颜色上进行绘制。

MaterialComponents 视图通常使用来自应用主题的值对图标进行着色,但可以禁用它。例如在 Button 类中,如果设置了app:iconTint="@null",则可以看到图标的原始颜色。我正在为 CheckBox 的 android:button 属性寻找一种方法,因为 buttonTint="@null" 似乎不起作用。这是布局文件,Button 显示预期的行为(图标是带有绿色复选标记的蓝色圆圈),而 CheckBox 在不应该的时候在这些颜色上绘制:

<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <Button
        android:id="@+id/button"
        android:layout_width="300dp"
        android:layout_height="60dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        android:text="Test button"
        android:backgroundTint="#000000"
        app:icon="@drawable/multi_color_icon"
        app:iconTint="@null"
        app:iconGravity="textStart"
        />

    <CheckBox
        android:id="@+id/checkbox"
        android:layout_width="wrap_content"
        android:layout_height="60dp"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@id/button"
        android:text="Test checkbox"
        android:gravity="center"
        android:button="@drawable/multi_color_icon"
        android:buttonTint="@null"
        android:paddingStart="8dp"
        android:paddingEnd="8dp"
        />

</androidx.constraintlayout.widget.ConstraintLayout>

可绘制资源:

<vector xmlns:android="http://schemas.android.com/apk/res/android"
    android:width="22dp"
    android:height="22dp"
    android:viewportWidth="22"
    android:viewportHeight="22">
  <path
      android:pathData="M11,22a11,11 0,1 1,11 -11,11 11,0 0,1 -11,11zM11,1.61A9.39,9.39 0,1 0,20.39 11,9.39 9.39,0 0,0 11,1.61z"
      android:fillColor="#0099FF"/>
  <path
      android:pathData="M9.364,15.885a0.829,0.829 0,0 1,-0.553 -0.218l-3.52,-3.247a0.829,0.829 0,1 1,1.106 -1.221l2.877,2.653 5.855,-7.093a0.829,0.829 0,0 1,1.279 1.056L9.997,15.584a0.829,0.829 0,0 1,-0.583 0.3z"
      android:fillColor="#98c02f"/>
</vector>

布局截图(复选框上的图标不应该是灰色的,而是像上面的按钮一样蓝/绿):

【问题讨论】:

    标签: android android-theme material-components-android android-checkbox material-components


    【解决方案1】:

    想不出办法让它与 MaterialComponents CheckBox 一起工作,所以我将布局改为使用 androidx.appcompat.widget.AppCompatCheckBox

    【讨论】:

      猜你喜欢
      • 2022-09-27
      • 2020-09-01
      • 1970-01-01
      • 2017-10-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多