Android MaterialButton在安卓棒棒糖版本中无法通过编程方式改变其颜色

4
我在一个线性布局(LinearLayout)中有两个材料风格的按钮(MaterialButton),当其中任意一个被点击时,根据一些内部状态我会改变它们的背景颜色和文本颜色。这在Lollipop以上的所有Android版本中都可以正常工作。初始状态在所有Android版本中都能够正常工作,问题出现在我在Lollipop中切换颜色时。以下是它们初始状态的照片。

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>

更改颜色的代码片段。

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()
}

请尝试使用com.google.android.material.button.MaterialButton代替android.support.design.button.MaterialButton - underoid
第一个是与androidX库一起使用的。但这里不是这种情况。 - Ahmed Abdelmeged
我建议您使用新库,而不是旧库,我认为这可能是问题所在。 - underoid
我在一个 Playground 应用程序中尝试了新的版本,但仍然存在相同的问题。 - Ahmed Abdelmeged
1
将xml中的backGroundTintList设置为selector可能会解决问题。 - underoid
1个回答

9
为文本和背景创建颜色状态选择器。对于文本的颜色,您可以执行以下操作:
<!--    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
请查看此问题,它也有类似的问题。这里是问题链接。

这是最好的解决方案。您也可以只使用 myButton.isSelected = true 来切换状态。 - GilbertS
android:state_checked 对我有效,而不是 android:state_selected。 - LutfiTekin

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,