【问题标题】:Material TextInputLayout styles are not working材质 TextInputLayout 样式不起作用
【发布时间】:2019-06-02 20:45:27
【问题描述】:

Material TextInputLayout 样式不起作用。

我在这里关注了关于材料 textinputlayout 的文档: https://material.io/develop/android/components/text-input-layout/

<com.google.android.material.textfield.TextInputLayout
    style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginStart="8dp"
    android:layout_marginTop="8dp"
    android:layout_marginEnd="8dp"
    android:hint="@string/app_name"
    app:boxStrokeColor="@android:color/white"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent">

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

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

使用 gradle:

implementation 'com.google.android.material:material:1.1.0-alpha02'

使用样式 OutlinedBox,我希望布局有一个轮廓,但它只显示一个纯 TextInputLayout。

【问题讨论】:

  • 试试implementation 'com.google.android.material:material:1.0.0'
  • 还是不行
  • @GabLedesma 已经在设备或 XML 中运行您的代码
  • @NileshRathod 当我运行它时它正在工作。但在预览中它不是。诡异的。无论如何,谢谢!

标签: android android-layout material-design


【解决方案1】:

它似乎无法在预览中运行,我必须在设备上运行它才能看到真正的输出。感谢@NileshRathod

希望安卓能解决这个问题

【讨论】:

【解决方案2】:

在我的假设中,问题出在 Appcompat 主题上。您必须将其更改为 materialCompontents.DayNight。

把你的 style.xml 文件改成这样,

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

并更改您的 TextInputLayout 代码如下:

   <com.google.android.material.textfield.TextInputLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="20dp"
        style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox">

        <com.google.android.material.textfield.TextInputEditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textColor="#252734"
            android:hint="Enter Name" />
    </com.google.android.material.textfield.TextInputLayout>

【讨论】:

  • 有时在android studio的ui设计器中不起作用。但它肯定会在真实设备上运行。
【解决方案3】:

将您的 styles.xml Theme.AppCompat.Light.NoActionBar 更改为 Theme.MaterialComponents.Light.NoActionBar

您可以使用以下代码:

    <style name="AppTheme" 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>
    
</resources>

【讨论】:

    【解决方案4】:

    它只适用于材质组件。 它不适用于 Theme.AppCompat,如果任何样式使用它,它将无法正常工作。 因此,将 style.xml 上的所有 Theme.AppCompat 更改为 Theme.MaterialComponents 喜欢

    Theme.MaterialComponents.Light.NoActionBar

    【讨论】:

      猜你喜欢
      • 2020-12-17
      • 2015-08-17
      • 2020-02-02
      • 1970-01-01
      • 1970-01-01
      • 2021-03-07
      • 2020-09-28
      • 1970-01-01
      • 2020-09-21
      相关资源
      最近更新 更多