首页 > 解决方案 > 为 App 设置自定义主题时 TextInputLayout 崩溃

问题描述

以下代码与大纲输入框完美配合。

<com.google.android.material.textfield.TextInputLayout
        android:id="@+id/notes"
        style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="@string/text_notes"
        app:boxBackgroundColor="@color/colorWhite">

        <com.google.android.material.textfield.TextInputEditText
            android:id="@+id/et_notes"
            android:fontFamily="sans-serif-black"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

    </com.google.android.material.textfield.TextInputLayout>

我在整个应用程序中设置了自定义字体。通过以下代码设置后,它正在崩溃。以下样式用于 App 主题。

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
    <item name="android:textViewStyle">@style/SansSerifTextViewStyle</item>// Custom font style
    <item name="buttonStyle">@style/SanSerifButtonStyle</item> // Custom font style
</style>

当我删除以下行时,应用程序不会崩溃。但概述的框 UI 不会出现。尝试通过Java代码设置,不工作。

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

标签: androidandroid-stylesandroid-textinputlayout

解决方案


材料组件,如com.google.android.material.textfield.TextInputLayout(注意.material.包名称的一部分),要求您使用材料主题。

此页面列出了要使用的适当主题:

https://github.com/material-components/material-components-android/blob/master/docs/getting-started.md#4-change-your-app-theme-to-inherit-from-a-material-组件主题

您必须从此更改应用程序的主题

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">

例如这个

<style name="AppTheme" parent="Theme.MaterialComponents.Light.DarkActionBar">

推荐阅读