【问题标题】:TextInputLayout Error Icon Drawable and Error Icon Tint not applying given styleTextInputLayout 错误图标可绘制和错误图标色调未应用给定样式
【发布时间】:2020-09-20 20:18:21
【问题描述】:

我在使用如下所示的样式时遇到了这个问题:

<style name="TextInputLayoutThemeWarning" parent="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox">
        <item name="boxStrokeErrorColor">@color/warning_color</item>
        <item name="errorTextColor">@color/warning_color</item>
        <item name="errorIconTint">@color/warning_color</item>
        <item name="errorIconDrawable">@drawable/ic_baseline_warning_24</item>
        <item name="textAppearanceCaption">@style/TextAppearanceTextInputLayout</item>
</style>

当它们在活动中时,它适用于任何 TextInputLayout,但是,当我在片段中的 TextInputLayout 上使用相同的样式时,不会应用可绘制和着色。我尝试以编程方式和 XML 代码手动设置 Drawable 和 Tint,似乎这两个属性被忽略并设置为默认值,而不是我选择的那些。

活动的主题是:

<style name="Theme.Default" parent="Theme.MaterialComponents.DayNight.NoActionBar">
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
        <item name="android:textViewStyle">@style/CondensedFont</item>
</style>

下面是片段布局中的一个 sn-p(为清楚起见而减少了):

<?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:id="@+id/clCCS1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/background">

    <ScrollView
        android:id="@+id/svCreateCharacter"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:fillViewport="true"
        android:scrollbarSize="0dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <androidx.constraintlayout.widget.ConstraintLayout
            android:id="@+id/clInnerContainer"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:visibility="visible">

            <com.google.android.material.textfield.TextInputLayout
                android:id="@+id/tilCharacterName"
                style="@style/TextInputLayoutThemeWarning"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_marginStart="8dp"
                android:layout_marginTop="4dp"
                android:layout_marginEnd="8dp"
                app:boxBackgroundColor="@color/content_background"
                app:errorEnabled="true"
                app:errorTextAppearance="@style/TextAppearanceTextInputLayout"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/tvCharacterTitle">

                <com.google.android.material.textfield.TextInputEditText
                    android:id="@+id/tietCharacterName"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:inputType="text"
                    android:lines="1"
                    android:maxLines="1"
                    android:textSize="12sp"
                    android:translationZ="4dp" />

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

【问题讨论】:

  • Activity使用的主题是什么?
  • 我已经更新了原来的问题,我用于活动的主题现在在原来的答案中。

标签: android xml kotlin


【解决方案1】:

可能会出现您遇到的问题,因为片段可能没有设置正确的样式,因为从我读过的内容来看,您必须单独设置片段的主题,这可以使用下面的代码在 onCreateView 函数中完成片段:

override fun onCreateView(
    inflater: LayoutInflater, container: ViewGroup?,
    savedInstanceState: Bundle?
): View? {
    val contextThemeWrapper: Context = ContextThemeWrapper(activity, R.style.yourCustomTheme)
    // clone the inflater using the ContextThemeWrapper
    val localInflater = inflater.cloneInContext(contextThemeWrapper)
    return localInflater.inflate(R.layout.fragment, container, false)
}

但是,为了让这个工作正常,您为片段设置的主题必须具有来自 Material Design Library 的父主题,根据我的经验和研究,如果活动或片段的父主题不是来自 Material Design Library可能会出现问题并且小部件将无法正常工作,应在您的样式文件中创建片段的自定义主题:

<style name="FragmentTheme"    parent="Theme.MaterialComponents.Light.NoActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
</style>

以下是一些我进行研究的有用资源的链接:

Set theme for a Fragment

InflateException when using TextInputLayout

【讨论】:

  • 我尝试使用它,但仍然无法将 errorIconTint 或 errorDrawable 更改为所需的。我已经更新了原始问题,任何帮助将不胜感激。
【解决方案2】:

我发现 Icon Tint 没有应用的原因不是因为样式没有应用,而是我不小心在我的 TextInputLayouts 中添加了一个平移 z 值。 'android:translationZ="4dp"' 会导致图标无法正常显示。

【讨论】:

    猜你喜欢
    • 2016-10-24
    • 2023-03-21
    • 2020-05-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-14
    • 1970-01-01
    • 2020-11-15
    相关资源
    最近更新 更多