为TextInputEditText配置OutlinedBox样式失败求助
解决TextInputLayout OutlinedBox样式配置问题
核心问题拆解
- 第一个错误:Android中不存在
android:style属性,设置样式直接用style即可;且OutlinedBox样式是给TextInputLayout容器用的,不是内部的TextInputEditText,你之前把样式加错了位置。 - 崩溃问题:你的App主题未继承Material Components主题,导致Material组件无法正常初始化,触发InflateException。
分步解决方案
1. 修正布局中的样式位置
将Widget.MaterialComponents.TextInputLayout.OutlinedBox样式添加到TextInputLayout标签上,移除EditText上的错误样式配置:
<com.google.android.material.textfield.TextInputLayout android:id="@+id/editLoginWrapper" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@+id/appTitle" android:hint="@string/login_hint" android:layout_marginTop="8dp" android:layout_marginBottom="8dp" style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"> <com.google.android.material.textfield.TextInputEditText android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/editLogin" android:inputType="text" /> </com.google.android.material.textfield.TextInputLayout>
密码输入框同理,给TextInputLayout加上OutlinedBox样式:
<com.google.android.material.textfield.TextInputLayout android:id="@+id/editPasswordWrapper" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@+id/editLoginWrapper" android:layout_marginTop="8dp" android:layout_marginBottom="8dp" android:hint="@string/password_hint" app:endIconMode="password_toggle" style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"> <com.google.android.material.textfield.TextInputEditText android:layout_width="match_parent" android:layout_height="wrap_content" android:inputType="textPassword" android:id="@+id/editPassword" /> </com.google.android.material.textfield.TextInputLayout>
2. 修正App主题(解决崩溃)
打开res/values/styles.xml,确保App主题继承自Material Components主题,示例:
<style name="AppTheme" parent="Theme.MaterialComponents.Light.NoActionBar"> <item name="colorPrimary">@color/colorPrimary</item> <item name="colorPrimaryDark">@color/colorPrimaryDark</item> <item name="colorAccent">@color/colorAccent</item> <!-- 其他主题配置 --> </style>
根据需求,也可选择Theme.MaterialComponents.Dark.NoActionBar等带ActionBar的变体。
3. 确认依赖配置
验证build.gradle(Module级别)中已正确引入Material组件依赖(你已配置,此处仅做确认):
implementation 'com.google.android.material:material:1.7.0'
关键注意事项
- 所有Material组件(如TextInputLayout)强制要求App主题继承Material Components主题,否则必然出现布局加载异常。
- OutlinedBox、FilledBox等样式属于容器样式,仅作用于TextInputLayout,内部EditText无需额外配置此类样式。
内容的提问来源于stack exchange,提问作者SanMiguel
相关产品推荐
相关产品推荐

