Ano*_*non 9 java android material-ui material-components-android
我想要实现的是拥有一个带有一些材质按钮的网格视图。我尝试创建网格视图,如下所示:
<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>
Run Code Online (Sandbox Code Playgroud)
要膨胀的元素如下:
<?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>
Run Code Online (Sandbox Code Playgroud)
适配器为按钮充气:
inflter = (LayoutInflater.from(applicationContext));
view = inflter.inflate(R.layout.button_code, null); // inflate the layout
Run Code Online (Sandbox Code Playgroud)
但我总是收到这个堆栈跟踪:
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).
Run Code Online (Sandbox Code Playgroud)
我检查了 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>
Run Code Online (Sandbox Code Playgroud)
有任何想法吗?
编辑:我尝试检查,我的应用程序有正确的主题
android:theme="@style/AppTheme"
Run Code Online (Sandbox Code Playgroud)
我还将主题更改为Theme.MaterialComponents.DayNight.NoActionBar.Bridge但没有任何改变
小智 6
改变你的主题
<style name="AppTheme" parent="Theme.MaterialComponents.DayNight.NoActionBar">
Run Code Online (Sandbox Code Playgroud)
到
<style name="AppTheme" parent="Theme.MaterialComponents.DayNight.NoActionBar.Bridge">
Run Code Online (Sandbox Code Playgroud)
问题就在这里。
inflter = (LayoutInflater.from(applicationContext));
Run Code Online (Sandbox Code Playgroud)
应用程序上下文没有您的应用程序主题。
您必须使用主题上下文,例如Activity.
| 归档时间: |
|
| 查看次数: |
6372 次 |
| 最近记录: |