【问题标题】:TextInputLayout Crashing when setting Custom Theme for App为 App 设置自定义主题时 TextInputLayout 崩溃
【发布时间】:2019-05-19 16:02:48
【问题描述】:

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

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

【问题讨论】:

    标签: android android-styles android-textinputlayout


    【解决方案1】:

    Material 组件,如 com.google.android.material.textfield.TextInputLayout(注意包名的 .material. 部分),要求您使用 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-components-theme

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

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

    例如这个

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

    【讨论】:

    • 谢谢!特定于活动我已经更改了主题和自定义字体也可以正常工作。
    • 设置此样式后,App工具栏标题不显示。我尝试使用 custum 样式更改颜色不起作用。任何想法...
    猜你喜欢
    • 1970-01-01
    • 2019-07-17
    • 1970-01-01
    • 2020-01-26
    • 1970-01-01
    • 1970-01-01
    • 2021-05-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多