android - 如何在不继承 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>
解决方案
我忽略了错误日志说里面的按钮MaterialButtonToggleGroup
必须是MaterialButton
. 当不继承 Material Components 主题时,我必须明确使用MaterialButton
类而不是Button
类。
推荐阅读
- c# - linq to sql的自定义函数
- ios - Swift Network.framework WebSocket 握手 nil 返回
- android-room - 主线程上的房间无效方法
- node.js - 为什么ubuntu服务器上的mongodb每天都会自动关闭?
- python - RPG脚本中的字典困难
- java - 尝试从字符串数组添加到列表时的 ArrayIndexOutOfBounds
- apache-kafka - 如何在 0.10.2.1 kafka 中修复 kafka 消费者滞后
- postgresql - 如果我在 Windows 上指定 -w,即使数据库处于恢复模式,我是否也必须等待启动完成?
- javascript - NodeJS 加密模块:干净的密码重用
- css - 在 ion-item 内的 ion-input 上方和下方显示文本