【问题标题】:Global style for TextInputLayout is not workingTextInputLayout 的全局样式不起作用
【发布时间】:2021-03-07 04:36:09
【问题描述】:

我想在TextInputLayout 上全局设置自定义样式为:

<resources xmlns:tools="http://schemas.android.com/tools">
    <!-- Base application theme. -->
    <style name="Theme.BudgetTracker" parent="Theme.MaterialComponents.DayNight.NoActionBar">
        <!-- Primary brand color. -->
        ...
        <!-- Secondary brand color. -->
        ...
        <!-- Status bar color. -->
        ...
        <!-- Customize your theme here. -->
        <item name="fontFamily">@font/regular</item>
        <!-- TextInputLayout-->
        <item name="textInputStyle">@style/Widget.MyNiceAppName.TextInputLayout.OutlinedBox</item>
    </style>

    <style name="Widget.MyNiceAppName.TextInputLayout.OutlinedBox" parent="Widget.MaterialComponents.TextInputLayout.OutlinedBox">
        <item name="android:imeOptions">actionNext</item>
        <item name="android:maxLines">1</item>
    </style>

</resources>

我的片段布局是

 <com.google.android.material.textfield.TextInputLayout
        android:id="@+id/outlinedTextField"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginStart="16dp"
        android:layout_marginEnd="16dp"
        android:layout_marginTop="32dp"
        android:hint="@string/label_email"
        android:inputType="textEmailAddress"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/acll">

        <com.google.android.material.textfield.TextInputEditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

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

但它永远不会起作用。非常感谢任何有关上述问题解决的建议。

【问题讨论】:

  • 我在我的应用程序中尝试了相同的代码,它成功了。是不是“maxlines”似乎不适合你?
  • 谢谢,但在我这边 android:imeOptionsandroid:maxLines 都不起作用。

标签: android material-design material-components-android android-textinputlayout


【解决方案1】:

您必须将这些属性应用于TextInputEditText 而不是TextInputLayout

在您的情况下,您可以使用:

<style name="Widget.MyNiceAppName.TextInputLayout.OutlinedBox" parent="Widget.MaterialComponents.TextInputLayout.OutlinedBox">
    <item name="materialThemeOverlay">
        @style/ThemeOverlay.App.TextInputEditText.OutlinedBox
    </item>
</style>

<style name="ThemeOverlay.App.TextInputEditText.OutlinedBox" parent="ThemeOverlay.MaterialComponents.TextInputEditText.OutlinedBox">
    <item name="editTextStyle">@style/Widget.App.TextInputEditText.OutlinedBox</item>
</style>

<style name="Widget.App.TextInputEditText.OutlinedBox" parent="Widget.MaterialComponents.TextInputEditText.OutlinedBox">
    <item name="android:imeOptions">actionNext</item>
    <item name="android:maxLines">1</item>
</style>

您也可以将android:inputType="textEmailAddress" 添加到TextInputEditText

   <com.google.android.material.textfield.TextInputEditText
        android:inputType="textEmailAddress"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

【讨论】:

    猜你喜欢
    • 2019-06-02
    • 2018-06-06
    • 2020-12-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-12-22
    相关资源
    最近更新 更多