问题
Material TextInputLayout styles are not working.
I followed the documentation on material textinputlayout here: 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>
With gradle:
implementation 'com.google.android.material:material:1.1.0-alpha02'
Using the style OutlinedBox, I expect to have the layout to have an outline but instead it just shows a plain TextInputLayout.
回答1:
It seems that it's not working on preview, I had to run it on a device to see the real output. Thanks to @NileshRathod
I hope that android fixes this
回答2:
In my assumption, The problem is due to the Appcompat theme. You must change it to materialCompontents.DayNight.
Change your style.xml file to like this,
<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>
And change your TextInputLayout Code as follows:
<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>
来源:https://stackoverflow.com/questions/54087043/material-textinputlayout-styles-are-not-working