gpt4 book ai didi

java - 错误膨胀类 com.google.android.material.button.MaterialButton

转载 作者:行者123 更新时间:2023-12-05 04:30:10 26 4
gpt4 key购买 nike

我想要实现的是拥有一个内部包含一些 materialButton 的 gridview。我尝试像这样创建 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>

适配器给按钮充气:

inflter = (LayoutInflater.from(applicationContext)); 
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

关于java - 错误膨胀类 com.google.android.material.button.MaterialButton,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72111416/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com