Android атрибут работает только от стиля? - PullRequest
1 голос
/ 26 января 2020

У меня есть 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>

1 Ответ

1 голос
/ 26 января 2020

Не существует такой вещи, как @style/Widget.MaterialComponents.TextInputEditText.OutlinedBox.

Однако существует стиль @style/Widget.MaterialComponents.TextInputLayout.OutlinedBox для TextInputLayout.

Атрибут, который вы непосредственно присвоили, был TextInputEditText.OutlinedBox. Это должно быть TextInputLayout.OutlinedBox

Обратите внимание, что editTextStyle нет. Причина, по которой он был применен на вашем style, заключается в том, что round_text имел parent="Base.Widget.MaterialComponents.TextInputLayout", который по умолчанию использует TextInputLayout.OutlinedBox как style.


В соответствии с Документами по проектированию материалов , вы должны сделать следующее:

<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>
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...