Material Android API 21 上的切换按钮问题
Material toggle button problem on Android API 21
我这样使用material切换按钮组
<com.google.android.material.button.MaterialButtonToggleGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:singleSelection="true">
<com.google.android.material.button.MaterialButton
android:id="@+id/btn_topso"
style="@style/Widget.MaterialComponents.Button.OutlinedButton"
android:layout_height="wrap_content"
android:layout_width="wrap_content" android:text="@string/topo" />
<com.google.android.material.button.MaterialButton
android:id="@+id/btn_averages"
style="@style/Widget.MaterialComponents.Button.OutlinedButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/average" />
</com.google.android.material.button.MaterialButtonToggleGroup>
但是当 运行 应用程序崩溃时出现此错误:
java.lang.IllegalStateException: Attempted to get ShapeAppearanceModel from a MaterialButton which has an overwritten background.
at com.google.android.material.button.MaterialButton.getShapeAppearanceModel(MaterialButton.java:1169)
at com.google.android.material.button.MaterialButtonToggleGroup.addView(MaterialButtonToggleGroup.java:244)
at android.view.ViewGroup.addView(ViewGroup.java:3709)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:810)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:809)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:809)
at android.view.LayoutInflater.inflate(LayoutInflater.java:504)
at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
我无法修复它!我该如何解决这个问题?
Material 版本:'1.3.0-alpha03'
风格:
<style name="MaterialAppTheme" parent="Theme.MaterialComponents.Light.NoActionBar">
<item name="colorPrimary">@color/primary</item>
<item name="colorPrimaryDark">@color/primaryDark</item>
<item name="colorAccent">@color/accent</item>
<item name="android:buttonStyle">@style/ButtonStyle</item>
<item name="android:background">@android:color/transparent</item>
<item name="floatingActionButtonStyle">@style/FloatingButtonStyle</item>
</style>
您正在使用:
<com.google.android.material.button.MaterialButton
android:background="@drawable/about"
使用 MaterialButton
中的 android:background
您将覆盖默认背景。这意味着 MaterialButton
不使用自己的 MaterialShapeDrawable
作为背景(和 ShapeAppearanceModel
)。
这似乎是一个错误,但您可以解决它,删除 MaterialButton
.
中的 android:background
我这样使用material切换按钮组
<com.google.android.material.button.MaterialButtonToggleGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:singleSelection="true">
<com.google.android.material.button.MaterialButton
android:id="@+id/btn_topso"
style="@style/Widget.MaterialComponents.Button.OutlinedButton"
android:layout_height="wrap_content"
android:layout_width="wrap_content" android:text="@string/topo" />
<com.google.android.material.button.MaterialButton
android:id="@+id/btn_averages"
style="@style/Widget.MaterialComponents.Button.OutlinedButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/average" />
</com.google.android.material.button.MaterialButtonToggleGroup>
但是当 运行 应用程序崩溃时出现此错误:
java.lang.IllegalStateException: Attempted to get ShapeAppearanceModel from a MaterialButton which has an overwritten background.
at com.google.android.material.button.MaterialButton.getShapeAppearanceModel(MaterialButton.java:1169)
at com.google.android.material.button.MaterialButtonToggleGroup.addView(MaterialButtonToggleGroup.java:244)
at android.view.ViewGroup.addView(ViewGroup.java:3709)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:810)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:809)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:809)
at android.view.LayoutInflater.inflate(LayoutInflater.java:504)
at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
我无法修复它!我该如何解决这个问题?
Material 版本:'1.3.0-alpha03'
风格:
<style name="MaterialAppTheme" parent="Theme.MaterialComponents.Light.NoActionBar">
<item name="colorPrimary">@color/primary</item>
<item name="colorPrimaryDark">@color/primaryDark</item>
<item name="colorAccent">@color/accent</item>
<item name="android:buttonStyle">@style/ButtonStyle</item>
<item name="android:background">@android:color/transparent</item>
<item name="floatingActionButtonStyle">@style/FloatingButtonStyle</item>
</style>
您正在使用:
<com.google.android.material.button.MaterialButton
android:background="@drawable/about"
使用 MaterialButton
中的 android:background
您将覆盖默认背景。这意味着 MaterialButton
不使用自己的 MaterialShapeDrawable
作为背景(和 ShapeAppearanceModel
)。
这似乎是一个错误,但您可以解决它,删除 MaterialButton
.
android:background