Android MaterialButton 不会在 android lollipop 中以编程方式更改它的颜色

标签 android xml android-layout colors material-design

我在 LinearLayout 中有两个 MaterialButton,我根据点击它们中的任何一个时切换到的某些内部状态更改它们的背景颜色和文本颜色。它在 Lollipop 以上的所有安卓版本中都运行良好。当我在 Lollipop 中切换颜色时,它们在所有 Android 版本中的初始状态都正常工作。

这是初始状态的照片。

enter image description here

Lollipop 以上所有 android 版本中正确颜色的照片。

enter image description here

Android Lollipop 目前发生的事情的照片。

enter image description here

我不知道那是什么情况。我尝试了很多不同的东西,但没有一个奏效。

这是布局文件的XML代码

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <android.support.design.button.MaterialButton
        android:id="@+id/handymanServicesButton"
        style="@style/View.RoundedMaterialButton"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_margin="@dimen/margin_medium"
        android:layout_weight="1"
        android:elevation="16dp"
        android:onClick="@{view::handymanServices}"
        android:text="@{viewModel.isHandymanUser() ? @string/handyman_services_button_label : @string/main_button_label}"
        android:textAllCaps="false"
        android:textSize="@dimen/text_size_small"
        tools:text="@string/handyman_services_button_label" />

    <android.support.design.button.MaterialButton
        android:id="@+id/otherButton"
        style="@style/RoundedMaterialButtonNotSelected"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_margin="@dimen/margin_medium"
        android:layout_weight="1"
        android:elevation="16dp"
        android:onClick="@{view::other}"
        android:text="@string/other_button_label"
        android:textAllCaps="false"
        android:textSize="@dimen/text_size_small" />

</LinearLayout>

我应用到按钮的样式。

<style name="View.RoundedMaterialButton">
    <item name="android:minHeight">@dimen/buttonHeight</item>
    <item name="android:elevation">@dimen/cardElevation</item>
    <item name="android:gravity">center</item>
    <item name="rippleColor">@color/material_ripple_color</item>
    <item name="cornerRadius">24dp</item>
    <item name="android:textSize">14sp</item>
    <item name="android:textStyle">bold</item>
    <item name="backgroundTint">@color/colorAccent</item>
    <item name="android:textAllCaps">true</item>
    <item name="android:textColor">@android:color/white</item>
    <item name="android:textAppearance">@style/TextAppearance.MaterialComponents.Button</item>
</style>

<style name="RoundedMaterialButtonNotSelected" parent="View.RoundedMaterialButton">
    <item name="backgroundTint">@android:color/white</item>
    <item name="android:textColor">@color/colorAccent</item>
</style>

fragment 中改变颜色的代码。

private fun handymanServicesUi() {
    binding.handymanServicesButton.backgroundTintList =
            ContextCompat.getColorStateList(requireContext(), R.color.colorAccent)
    binding.otherButton.backgroundTintList =
            ContextCompat.getColorStateList(requireContext(), android.R.color.white)

    binding.handymanServicesButton.setTextColor(Color.WHITE)
    binding.otherButton.setTextColor(
            ContextCompat.getColor(requireContext(), R.color.colorAccent))
}

fun handymanServices(@Suppress("UNUSED_PARAMETER") view: View) {
    handymanServicesUi()
    viewModel.switchToHandymanServices()
}

private fun otherUi() {
    binding.handymanServicesButton.backgroundTintList =
            ContextCompat.getColorStateList(requireContext(), android.R.color.white)
    binding.otherButton.backgroundTintList =
            ContextCompat.getColorStateList(requireContext(), R.color.colorAccent)

    binding.handymanServicesButton.setTextColor(
            ContextCompat.getColor(requireContext(), R.color.colorAccent))
    binding.otherButton.setTextColor(Color.WHITE)
}

fun other(@Suppress("UNUSED_PARAMETER") view: View) {
    otherUi()
    viewModel.switchToOthers()
}

最佳答案

为文本和背景创建颜色状态选择器。对于文本的颜色,您可以执行以下操作:

<!--    under res/colors/handyman_text_color_selector.xml-->

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:color="@color/colorAccent" android:state_selected="true" />
    <item android:color="@android:color/white" />
</selector>

对“backgroundTint”做同样的事情,比如 handyman_button_color_selector.xml

接下来将它们应用于布局并使用这些颜色选择器。 (最好将它们添加到样式中)

<android.support.design.button.MaterialButton
    android:id="@+id/handymanServicesButton"
    style="@style/View.RoundedMaterialButton"

    android:textColor="@color/handyman_text_color_selector"
    app:backgroundTint="@color/handyman_button_color_selector" />

最后在 Kotlin 中,只需切换两个按钮:

binding.handymanServicesButton.isSelected = !isSelected
binding.otherButton.isSelected = isSelected

请查看此 issue它也有类似的问题。

关于Android MaterialButton 不会在 android lollipop 中以编程方式更改它的颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55557372/

相关文章:

java - Android 中 ImageButton 上的 ClickEvent?

java - 如何在java中获取元素的标签类型

xml - 如何将 xml 属性添加到 jaxb 注释类 XmlElementWrapper?

android - recyclerView 的 setOnClickListener 不起作用

android - 是否需要给每个Widget(TextView、LinearLayout...)一个Id?

java - 信息窗口中的 OSM 中心十

java - Android 并获取将 id 转换为字符串的 View

java - 简单的 Android 应用程序在启动时崩溃

android - 为什么我的评论会出错?

android - 相对布局 : button pushes other button off screen