Не изменять стиль на лету с помощью TextViewCompat.setTextAppearance

В Android Studio 3.5 Android 6.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
                )

Но в результате он меняет только цвет текста. Но цвет фона не меняется




Ответы (1)


Метод 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>

Прочтите эту публикацию, чтобы узнать больше.

person Gabriele Mariotti    schedule 22.11.2019