如何在不继承 Material Components 主题的情况下使用 MaterialButtonToggleGroup

How to use MaterialButtonToggleGroup without inheriting from a Material Components theme

我想创建 MaterialButtonToggleGroup 而不让我的 AppTheme 继承 Theme.MaterialComponents.Light.DarkActionBar。所以我尝试继承 Theme.MaterialComponents.Light.DarkActionBar.BridgeMaterialButtonToggleGroup 完全消失了,在视图上没有显示任何内容。当我删除 .Bridge 时它确实有效。如何在不继承 Material 组件主题的情况下正确使用 MaterialButtonToggleGroup

styles.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>

我忽略了错误日志说 MaterialButtonToggleGroup 里面的按钮必须是 MaterialButton。当不继承 Material Components 主题时,我必须明确使用 MaterialButton class 而不是 Button class.