Android Studio TextInputEditText的大纲框-属性android:未找到样式

rm5edbpk  于 2022-11-25  发布在  Android
关注(0)|答案(1)|浏览(195)

无法获取TextInputEditText的工作OutlinedBox。Stackoverflow上存在similar question,但所有解决方案都不起作用。
只要我添加android:style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"到代码中,表单就会从预览中消失,当我尝试构建apk时,会出现以下错误:

Android resource linking failed
com.my.app-main-50:/layout/fragment_login.xml:32: error: attribute android:style not found.
error: failed linking file resources.

只需将style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
也不起作用
screenshot

构建版本:

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

代码:

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:background="#EAEAEA"
    tools:context=".LoginFragment">

    <androidx.appcompat.widget.AppCompatImageView
        android:layout_width="match_parent"
        android:layout_height="80dp"
        android:src="@drawable/logo_src"
        android:layout_alignParentStart="true"
        android:id="@+id/imageLogo"
        android:layout_alignParentTop="true"
        android:contentDescription="@string/t_logo"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="@string/app_description"
        android:id="@+id/appTitle"
        android:layout_below="@+id/imageLogo"
        android:layout_marginBottom="8dp"
        android:layout_centerHorizontal="true" />

    <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">
        <com.google.android.material.textfield.TextInputEditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
            android:id="@+id/editLogin"
            android:inputType="text" />
    </com.google.android.material.textfield.TextInputLayout>

    <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">
        <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>

    <androidx.appcompat.widget.AppCompatButton
        android:id="@+id/signIn"
        style="?attr/buttonStyle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/sign_in"
        android:layout_below="@+id/editPasswordWrapper"
        android:textSize="@dimen/button_text"
        android:textColor="@color/white"
        android:enabled="true"
        android:layout_centerHorizontal="true"
        app:backgroundTint="@color/button_background" />

    <Button
        android:id="@+id/forgotPassword"
        style="?attr/buttonBarButtonStyle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/signIn"
        android:layout_centerHorizontal="true"
        android:text="@string/forgot_password"
        android:textColor="#918B8B"
        android:textColorHighlight="#DC6C6C"
        android:textColorLink="#5E5E5E" />

</RelativeLayout>

文本输入布局内的样式:

<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"
    style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
    android:layout_marginBottom="8dp">

应用程序崩溃:

FATAL EXCEPTION: main
android.view.InflateException: Binary XML file line #41 in com.app.app:layout/fragment_login: Binary XML file line #41 in com.app.app:layout/fragment_login: Error inflating class com.google.android.material.textfield.TextInputLayout
Caused by: android.view.InflateException: Binary XML file line #41 in com.app.app:layout/fragment_login: Error inflating class com.google.android.material.textfield.TextInputLayout

第41行为:

style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
y3bcpkx1

y3bcpkx11#

TextInputLayout中添加样式而不是TextInputEditText

style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"

并将主题添加到根布局,您的活动主题应为MaterialComponents

android:theme="@style/Theme.MaterialComponents.Light"

相关问题