Как использовать MaterialButtonToggleGroup без наследования от темы Material Components - PullRequest
0 голосов
/ 29 апреля 2020

Я хочу создать MaterialButtonToggleGroup без наследования моего AppTheme Theme.MaterialComponents.Light.DarkActionBar. Поэтому я попытался унаследовать Theme.MaterialComponents.Light.DarkActionBar.Bridge, но затем MaterialButtonToggleGroup полностью исчез, ничего не показывается в представлении. Это работает, когда я удаляю .Bridge. Как правильно использовать MaterialButtonToggleGroup без наследования от темы компонентов материалов?

стилей. xml

<resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.MaterialComponents.Light.DarkActionBar.Bridge">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
        <item name="borderlessButtonStyle">@style/Widget.App.Button.TextButton</item>
        <item name="materialButtonOutlinedStyle">@style/Widget.App.Button.OutlinedButton</item>
        <item name="materialButtonStyle">@style/Widget.App.Button</item>
    </style>

    <style name="Widget.App.Button.TextButton" parent="Widget.MaterialComponents.Button.TextButton">
        <item name="materialThemeOverlay">@style/ThemeOverlay.App.Button.TextButton</item>
        <item name="android:textAppearance">@style/TextAppearance.App.Button</item>
        <item name="shapeAppearance">@style/ShapeAppearance.App.SmallComponent</item>
    </style>

    <style name="Widget.App.Button.OutlinedButton" parent="Widget.MaterialComponents.Button.OutlinedButton">
        <item name="materialThemeOverlay">@style/ThemeOverlay.App.Button.TextButton</item>
        <item name="android:textAppearance">@style/TextAppearance.App.Button</item>
        <item name="shapeAppearance">@style/ShapeAppearance.App.SmallComponent</item>
    </style>

    <style name="Widget.App.Button" parent="Widget.MaterialComponents.Button">
        <item name="materialThemeOverlay">@style/ThemeOverlay.App.Button</item>
        <item name="android:textAppearance">@style/TextAppearance.App.Button</item>
        <item name="shapeAppearance">@style/ShapeAppearance.App.SmallComponent</item>
    </style>

    <style name="ThemeOverlay.App.Button.TextButton" parent="">
        <item name="colorPrimary">@color/colorPrimary</item>
    </style>

    <style name="ThemeOverlay.App.Button" parent="">
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorOnPrimary">#ffffff</item>
    </style>

    <style name="TextAppearance.App.Button" parent="TextAppearance.MaterialComponents.Button">
        <item name="fontFamily">serif</item>
        <item name="android:fontFamily">serif</item>
    </style>

    <style name="ShapeAppearance.App.SmallComponent" parent="ShapeAppearance.MaterialComponents.SmallComponent">
        <item name="cornerFamily">cut</item>
        <item name="cornerSize">4dp</item>
    </style>

</resources>

activity_main. xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <com.google.android.material.button.MaterialButtonToggleGroup
        android:id="@+id/toggleButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
        <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button 1"
            style="@style/Widget.App.Button.OutlinedButton"
            />
        <Button
            android:id="@+id/button2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button 2"
            style="@style/Widget.App.Button.OutlinedButton"
            />
        <Button
            android:id="@+id/button3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button 3"
            style="@style/Widget.App.Button.OutlinedButton"
            />
    </com.google.android.material.button.MaterialButtonToggleGroup>

</LinearLayout>

1 Ответ

0 голосов
/ 04 мая 2020

Я упустил из виду, что в журнале ошибок сказано, что кнопки внутри MaterialButtonToggleGroup должны быть MaterialButton. Когда не наследуется тема Material Components, я должен явно использовать класс MaterialButton вместо Button class.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...