TextViewCompat.setTextAppearance 未更改样式 "on fly"

Not change style "on fly" by TextViewCompat.setTextAppearance

在 Android Studio 3.5 中 Android6.0

这是我的风格:

 <style name="buttonEnableStyle" parent="Widget.MaterialComponents.Button.UnelevatedButton">
        <item name="android:textColor">@color/text_color_states_materialbuttons</item>
        <item name="backgroundTint">@color/color_states_materialbutton</item>
        <item name="android:textAppearance">@style/byttonTexAppearanceStyle</item>
    </style>



  <style name="buttonClickStyle" parent="@style/Widget.MaterialComponents.Button">
        <item name="android:textColor">#ffc400</item>
        <item name="backgroundTint">#3F3F3F</item>
    </style>

xml布局:

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <data>

        <import type="android.view.View" />       

    </data>


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

我尝试像这样以编程方式更改 MaterialButton 的样式:

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

但结果它只改变了文本颜色。但是背景颜色没有改变

方法 setTextAppearance textAppearance Button 的背景或样式.

要更改 Button 中的颜色和文本外观,您可以使用类似的东西:

//For background color
button.setBackgroundTintList(ContextCompat.getColorStateList(this,R.color...));
// or button.setBackgroundColor(ContextCompat.getColor(this,R.color....))

// For TextAppearance
button.setTextAppearance(..);

关于 TextAppearance 的注释。
它定义了这些属性:

<declare-styleable name="TextAppearance">
    <!-- Text color. -->
    <attr name="textColor" />
    <!-- Size of the text. Recommended dimension type for text is "sp" for scaled-pixels (example: 15sp). -->
    <attr name="textSize" />
    <!-- Style (normal, bold, italic, bold|italic) for the text. -->
    <attr name="textStyle" />
    <!-- Weight for the font used in the TextView. -->
    <attr name="textFontWeight" />
    <!-- Typeface (normal, sans, serif, monospace) for the text. -->
    <attr name="typeface" />
    <!-- Font family (named by string or as a font resource reference) for the text. -->
    <attr name="fontFamily" />
    <!-- Specifies the {@link android.os.LocaleList} for the text.
         May be a string value, which is a comma-separated language tag list, such as "ja-JP,zh-CN".
         When not specified or an empty string is given, it will fallback to the default one.
         {@see android.os.LocaleList#forLanguageTags(String)} -->
    <attr name="textLocale" format="string" />
    <!-- Color of the text selection highlight. -->
    <attr name="textColorHighlight" />
    <!-- Color of the hint text. -->
    <attr name="textColorHint" />
    <!-- Color of the links. -->
    <attr name="textColorLink" />
    <!-- Present the text in ALL CAPS. This may use a small-caps form when available. -->
    <attr name="textAllCaps" format="boolean" />
    <!-- Place a blurred shadow of text underneath the text, drawn with the
         specified color. The text shadow produced does not interact with
         properties on View that are responsible for real time shadows,
         {@link android.R.styleable#View_elevation elevation} and
         {@link android.R.styleable#View_translationZ translationZ}. -->
    <attr name="shadowColor" format="color" />
    <!-- Horizontal offset of the text shadow. -->
    <attr name="shadowDx" format="float" />
    <!-- Vertical offset of the text shadow. -->
    <attr name="shadowDy" format="float" />
    <!-- Blur radius of the text shadow. -->
    <attr name="shadowRadius" format="float" />
    <!-- Elegant text height, especially for less compacted complex script text. -->
    <attr name="elegantTextHeight" format="boolean" />
    <!-- Whether to respect the ascent and descent of the fallback fonts that are used in
    displaying the text. When true, fallback fonts that end up getting used can increase
    the ascent and descent of the lines that they are used on. -->
    <attr name="fallbackLineSpacing" format="boolean"/>
    <!-- Text letter-spacing. -->
    <attr name="letterSpacing" format="float" />
    <!-- Font feature settings. -->
    <attr name="fontFeatureSettings" format="string" />
    <!-- Font variation settings. -->
    <attr name="fontVariationSettings" format="string"/>
</declare-styleable>

查看 this post 了解更多详情。