首页 > 解决方案 > 如何在不继承 Material Components 主题的情况下使用 MaterialButtonToggleGroup

问题描述

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

样式.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>

标签: androidmaterial-design

解决方案


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


推荐阅读