You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

添加OutlinedBox样式致TextInputLayout崩溃问题求助

解决添加OutlinedBox样式后App崩溃的问题

核心原因

Widget.MaterialComponents.TextInputLayout.OutlinedBox是Material Components专属样式,要求App的主题必须继承自Material Components系列主题,否则会因找不到样式依赖的属性引发崩溃;另外原布局中TextInputLayout的高度设置不合理也可能加剧冲突。


具体修复步骤

  1. 修改App主题
    打开res/values/styles.xml,确保应用主题继承自Material Components主题,示例:

    <style name="AppTheme" parent="Theme.MaterialComponents.Light.NoActionBar">
        <!-- 保留原有主题属性,如颜色、标题栏设置等 -->
    </style>
    

    深色主题可替换为Theme.MaterialComponents.Dark.NoActionBar。

  2. 修正TextInputLayout布局属性
    将原代码中TextInputLayout的android:layout_height="match_parent"改为wrap_content,并添加OutlinedBox样式:

    <com.google.android.material.textfield.TextInputLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox">
    
  3. 确认Material Components依赖
    检查模块级build.gradle文件,确保已添加Material Components依赖(版本建议与AndroidX组件版本匹配):

    implementation 'com.google.android.material:material:1.9.0'
    

修复后的完整XML代码

<?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"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/ic_bg"
    tools:context=".MainActivity">

    <androidx.cardview.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginStart="20dp"
        android:layout_marginEnd="20dp"
        android:layout_marginTop="30dp"
        android:elevation="5dp"
        app:cardCornerRadius="8dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textView"
        app:layout_constraintVertical_bias="0.0">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:padding="16dp">

            <com.google.android.material.textfield.TextInputLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="20dp"
                style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox">

                <com.google.android.material.textfield.TextInputEditText
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:hint="Name"
                    android:textColor="#303A43"
                    android:textColorHint="#7A8089"/>
            </com.google.android.material.textfield.TextInputLayout>

        </LinearLayout>

    </androidx.cardview.widget.CardView>

</androidx.constraintlayout.widget.ConstraintLayout>

内容的提问来源于stack exchange,提问作者Sneakoz

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.22 07:24:22