【问题标题】:How to change background color of Android Material ChipGroup?如何更改 Android Material ChipGroup 的背景颜色?
【发布时间】:2020-07-03 06:37:54
【问题描述】:

我需要动态更改 Android Material 芯片容器 (ChipGroup) 组件的背景颜色。我试过setBackground()setBackgroundTint()。我还尝试了 XML 中的 android:backgroundandroid:backgroundTint 属性(下面的代码)只是为了检查。但他们也没有工作。知道怎么做吗?

<com.google.android.material.chip.ChipGroup
    android:id="@+id/chips"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginBottom="0dp"
    android:background="@color/tw__transparent"
    android:backgroundTint="@color/tw__transparent"
    app:ensureMinTouchTargetSize="false"
    app:chipSpacing="2dp"/>

【问题讨论】:

    标签: android material-design background-color android-chips


    【解决方案1】:

    在 java 文件中试试这个。

    // Defined in colors.xml - #ca8eed
    chip.setChipBackgroundColorResource(R.color.chip)
    

    如果你想改变textColor,你可以使用style

    在styles.xml文件中,

    <resources>
       ...
        <style name="ChipTextStyle">
            <item name="android:textSize">18sp</item>
            <item name="android:textColor">#2b90ff</item>
        </style>
      ...
    

    并在布局xml文件中添加样式。

    【讨论】:

    • 我认为 Kulakshi 要求更改 ChipGroup 背景颜色而不是芯片背景颜色
    • 哦。对不起。然后只需覆盖其他布局并在覆盖布局中设置背景颜色是简单的方法。我认为
    【解决方案2】:

    ChipGroup 只是一个ViewGroup。您可以使用:

        <com.google.android.material.chip.ChipGroup
            android:background="@color/...."
    

    或者您可以使用 setBackgroundColor 方法:

    chipGroup.setBackgroundColor(ContextCompat.getColor(this,R.color...));
    

    【讨论】:

      【解决方案3】:

      您可以设置包括芯片在内的布局(线性或相对)并将背景颜色设置为该布局,简单的方法。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-07-01
        • 2021-12-01
        • 2021-08-01
        • 2017-03-28
        • 2020-11-26
        • 2014-11-08
        • 2019-11-14
        • 2017-09-03
        相关资源
        最近更新 更多