【问题标题】:Android attribute works only from style?Android 属性仅适用于样式?
【发布时间】:2020-05-12 04:53:18
【问题描述】:

我有 textinputlayout 并想为其添加一个属性,但它仅在从样式分配时才有效

我的风格

 <style name="round_text" parent="Base.Widget.MaterialComponents.TextInputLayout">
    <item name="editTextStyle">@style/Widget.MaterialComponents.TextInputEditText.OutlinedBox</item>
</style>

我对风格的看法

 <com.google.android.material.textfield.TextInputLayout
    android:id="@+id/text_input"
    android:theme="?attr/textInputStyle"
    style="@style/round_text"
    android:layout_width="300dp"
android:layout_centerInParent="true"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    android:layout_height="wrap_content">

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

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

我的视图没有直接分配样式和属性

 <com.google.android.material.textfield.TextInputLayout
    android:id="@+id/text_input"
    android:theme="?attr/textInputStyle"
    android:editTextStyle="@style/Widget.MaterialComponents.TextInputEditText.OutlinedBox"
    android:layout_width="300dp"
android:layout_centerInParent="true"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    android:layout_height="wrap_content">

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

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

【问题讨论】:

  • 我的回答应该准确解释为什么它不起作用。希望对您有所帮助。

标签: android xml styles


【解决方案1】:

没有@style/Widget.MaterialComponents.TextInputEditText.OutlinedBox这样的东西。

不过,TextInputLayout@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox 样式。

您直接分配的属性是TextInputEditText.OutlinedBox应该是TextInputLayout.OutlinedBox

请注意没有editTextStyle。从您的style 应用它的原因是因为round_text 具有parent="Base.Widget.MaterialComponents.TextInputLayout",默认情况下使用TextInputLayout.OutlinedBox 作为style


根据Material Design Docs,您应该执行以下操作:

<com.google.android.material.textfield.TextInputLayout
    android:id="@+id/text_input"
    android:theme="?attr/textInputStyle"
    style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
    android:layout_width="300dp"
    android:layout_centerInParent="true"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    android:layout_height="wrap_content">

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

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

【讨论】:

  • 感谢您的回答,它确实对我有很大帮助,但我注意到奇怪的是这种风格只适用于风格,但不适用于主题 android:theme="@style/round_text" 你能解释一下吗我?
  • 好的,所以TextInputLayoutTextInputEditText 都是自定义布局。而您的 theme : @style/round_text 正在尝试更改 editTextStyle。但是editTextStyle 只影响EditText 的样式,而不影响TextInputLayout(实际上是围绕它的边界和一切)。您无法设置TextInputLayoutstyle,因为它是自定义视图。所以你必须从 xml 中设置它。
  • 你能给我一个这些属性的例子吗?
  • 非常感谢你帮了我很多。我试图回答这个问题 (stackoverflow.com/questions/59918896/…) 并发现自己在问另一个问题,所以试图回答问题让我更深入地挖掘并学习新事物。谢谢:)
  • 感谢您的提问 :)
猜你喜欢
  • 2021-04-07
  • 2015-02-11
  • 1970-01-01
  • 2022-08-11
  • 2022-01-27
  • 1970-01-01
  • 2017-02-25
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多