![](/img/trans.png)
[英]Binary XML file line #10: Error inflating class com.google.android.material.button.MaterialButton for API Level 19
[英]Error inflating class com.google.android.material.button.MaterialButton
我想要實現的是有一個 gridview 里面有一些 materialButton。 我試圖創建 gridview 像:
<GridView
android:id="@+id/login_gridview_code_input"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:numColumns="3"
android:horizontalSpacing="15dp"
android:verticalSpacing="15dp">
</GridView>
並且要膨脹的元素如下:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.google.android.material.button.MaterialButton
android:id="@+id/button_code_digit"
style="@style/Widget.MaterialComponents.Button.OutlinedButton.Icon"
android:layout_width="50dp"
android:layout_height="50dp"
android:insetLeft="0dp"
android:insetTop="0dp"
android:insetRight="0dp"
android:insetBottom="0dp"
android:padding="0dp"
app:iconGravity="textStart"
app:iconPadding="0dp"
app:iconSize="40dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:shapeAppearanceOverlay="@style/ShapeAppearanceOverlay.pswStorer.Button.Circle" />
</androidx.constraintlayout.widget.ConstraintLayout>
適配器為按鈕充氣:
view = inflter.inflate(R.layout.button_code, null); // inflate the layout
但我總是收到這個堆棧跟蹤:
Error inflating class com.google.android.material.button.MaterialButton
Caused by: java.lang.IllegalArgumentException: The style on this component requires your app theme to be Theme.MaterialComponents (or a descendant).
我檢查了 appTheme 但對我來說似乎是正確的:
<style name="AppTheme" parent="Theme.MaterialComponents.DayNight.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
<item name="materialAlertDialogTheme">@style/AlertDialogMaterialTheme</item>
</style>
有任何想法嗎?
編輯:我嘗試檢查並且我的應用具有正確的主題
android:theme="@style/AppTheme"
我還將主題更改為Theme.MaterialComponents.DayNight.NoActionBar.Bridge但沒有任何改變
問題就在這里。
inflter = (LayoutInflater.from(applicationContext));
應用程序上下文沒有您的應用程序主題。
您必須使用像Activity
這樣的主題上下文。
檢查您的 AndroidManifest.xml 您是否也在為您的應用使用正確的主題
<application
android:theme="@style/AppTheme"
...>
改變你的主題
<style name="AppTheme" parent="Theme.MaterialComponents.DayNight.NoActionBar">
至
<style name="AppTheme" parent="Theme.MaterialComponents.DayNight.NoActionBar.Bridge">
如果你有一個單獨的夜間主題,別忘了也改成 Theme.MaterialComponents 主題。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.