我想实现的目标是创建一个带有一些材质按钮的网格视图。
我试着像这样创建网格视图:
适配器填充按钮。
<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
? - Gabriele Mariotti