How to apply global custom style to android.support.design.widget.TextInputEditText?

二次信任 提交于 2019-12-03 06:21:35

The new MDC components allow a lot of flexibility here but it is still quite strange. I wanted a custom font for all TextInputEditTexts, and the filled (default) style. The only way it would work for me is by specifying the font on the TextInputEditText using the global style attribute:

<!-- Base application theme. -->
<style name="MyAppTheme" parent="Theme.MaterialComponents.Light.NoActionBar">
    <item name="textInputStyle">@style/MyTextField.Outlined</item>
    <item name="editTextStyle">@style/MyEditTextFont</item>
</style>

<style name="MyEditTextFont" parent="Widget.MaterialComponents.TextInputEditText.FilledBox">
    <item name="android:fontFamily">@font/my_font</item>
</style>
<style name="MyTextField" parent="Widget.MaterialComponents.TextInputLayout.FilledBox"/>
<style name="MyTextField.Outlined" parent="Widget.MaterialComponents.TextInputLayout.OutlinedBox"/>

If for example you don't specify the editTextStyle, then the textInputStyle does work but has crazy missing padding. Anyway I recommend using both of these fields and styling them both as necessary.

With the Material theme you can use the textInputStyle to style the com.google.android.material.textfield.TextInputLayout component.

<style name="AppTheme" parent="Theme.MaterialComponents.Light">
   ....
   <item name="textInputStyle">@style/MyTextInputLayoutStyle</item>
</style>

<style name=MyTextInputLayoutStyle" parent="@style/Widget.MaterialComponents.TextInputLayout.FilledBox">
     ....
</style>

If you would like to customize the TextInputEditText just add:

<style name=MyTextInputLayoutStyle" parent="@style/Widget.MaterialComponents.TextInputLayout.FilledBox">
     ....
    <item name="materialThemeOverlay">
      @style/MyThemeOverlayTextInputEditTextFilledBox</item>
</style>

<style name="MyThemeOverlayTextInputEditTextFilledBox">
   <item name="editTextStyle">@style/MyTextInputEditText</item>
</style>

For example:

  <style name="MyTextInputEditText" parent="@style/Widget.MaterialComponents.TextInputEditText.FilledBox">
    <item name="android:paddingTop">8dp</item>
    <item name="android:paddingBottom">8dp</item>
    <item name="android:textAppearance">myTextAppearance</item>
    ...
  </style>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!