【问题标题】:Can't change backgroundTint programmatically无法以编程方式更改 backgroundTint
【发布时间】:2019-10-27 15:22:12
【问题描述】:

Android Studio 3.6

我的风格。 xml

 <style name="buttonStyle">
        <item name="android:textColor">@color/default_button_textColor</item>
        <item name="android:backgroundTint">@color/button_bg_color</item>
        <item name="android:textSize">18sp</item>
        <item name="android:textAllCaps">true</item>
    </style>

    <style name="buttonClickStyle">
        <item name="android:textColor">@color/button_bg_color</item>
        <item name="android:backgroundTint">@color/button_click_bg_color</item>
        <item name="android:textSize">18sp</item>
        <item name="android:textAllCaps">true</item>
    </style>

在xml布局中:

 <com.google.android.material.button.MaterialButton
            android:id="@+id/buttonStartSearchBluetooth"
            style="@style/buttonStyle"
            android:layout_width="0dp"
            android:layout_height="@dimen/button_height"
            android:layout_margin="@dimen/button_margin"
            android:onClick="onClickButtonStartSearch"
            android:text="@string/start_search"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent" />

当点击按钮时,我尝试像这样改变样式:

dataBinding.buttonStartSearchBluetooth.setTextAppearance(R.style.buttonClickStyle)

但它只改变文本颜色。不改backgroundTint

为什么?

【问题讨论】:

    标签: android android-button android-styles material-components material-components-android


    【解决方案1】:

    您的风格存在一些问题。

    • 使用MaterialComponents.Button.* style 作为父级
    • 使用backgroundTint 而不是android:backgroundTint
    • 使用android:textAppearance 定义您的文本样式

    类似:

    <style name="buttonStyle" parent="@style/Widget.MaterialComponents.Button">
      <item name="android:textColor">@color/default_button_textColor</item>
      <item name="backgroundTint">@color/my_selector</item>
      <item name="android:textAppearance">@style/my_textAppearance</item>
    </style>
    <style name="my_textAppearance" parent="@style/TextAppearance.MaterialComponents.Button">
        <item name="android:textSize">18sp</item>
        <item name="android:textAllCaps">true</item>
    </style>
    

    最后以编程方式更改文本颜色和背景颜色使用:

    类似:

        button.setBackgroundTintList(ContextCompat.getColorStateList(this,R.color.button_selector));
        button.setTextColor(ContextCompat.getColor(this, R.color....));
    

    【讨论】:

    • 您忘记使用“button.setTextAppearance(R.style.buttonStyle)”“on fly”更改按钮的文本颜色
    • @a_subscriber 在您的问题中,您询问了如何更改背景颜色。在任何情况下都使用button.setTextColor(ContextCompat.getColor(this, R.color....));
    【解决方案2】:

    试试这个代码

    yourbutton.setBackgroundTintList(contextInstance.getResources().getColorStateList(R.color.your_xml_name));
    

    【讨论】:

      【解决方案3】:

      使用此代码更改 MaterialComponents 按钮背景颜色。

      yourbutton.setBackgroundTintList(ColorStateList.valueOf(Color.parseColor(color)));
      

      【讨论】:

      • 这就是答案!
      【解决方案4】:

      setTextAppearance 只关注 TextView 的 Text 样式而不是背景。检查official doc 以查看受setTextAppearance 影响的受支持属性。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-08-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多