我在AppTheme
中定义了style.xml
,我为我的所有应用TextView
,EditText
,Button
全局应用了自定义样式等
现在我想对android.support.design.widget.TextInputEditText
做同样的事情,但是怎么做?
它不会扩展EditText
,因此会忽略该样式。
以下是我所拥有的:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="AppTheme" parent="@style/Theme.AppCompat.Light.NoActionBar">
<item name="colorPrimary">@color/primary</item>
<item name="colorPrimaryDark">@color/primary</item>
<item name="colorAccent">@color/accent</item>
<!-- Globally override default widget styles with custom -->
<item name="android:textViewStyle">@style/App.TextView</item>
<item name="android:editTextStyle">@style/App.EditText</item>
<item name="android:buttonStyle">@style/App.Button.Primary</item>
</style>
<style name="App.TextView" parent="@android:style/Widget.TextView">
<item name="android:textColor">@color/text</item>
<item name="android:fontFamily">sans-serif-light</item>
<item name="android:textSize">@dimen/textsize_normal</item>
</style>
<style name="App.EditText" parent="@android:style/Widget.EditText">
<item name="android:textColor">#f00</item>
<item name="android:fontFamily">sans-serif-light</item>
<item name="android:textSize">@dimen/textsize_xxxlarge</item>
<item name="android:textStyle">bold</item>
<item name="android:textColorHint">@color/text_hint</item>
</style>
<style name="App.Button" parent="@android:style/Widget.Button">
<item name="android:textSize">@dimen/button_text_size</item>
<item name="android:fontFamily">sans-serif-medium</item>
<item name="android:textAllCaps">false</item>
</style>
<style name="App.Button.Primary">
<item name="android:background">@drawable/button_primary_background</item>
<item name="android:height">@dimen/button_primary_height</item>
<item name="android:width">@dimen/button_primary_width</item>
<item name="android:paddingLeft">@dimen/padding_normal</item>
<item name="android:paddingRight">@dimen/padding_normal</item>
<item name="android:textColor">@color/button_primary_text</item>
</style>
...
答案 0 :(得分:1)
使用Material主题,您可以使用 textInputStyle
为 com.google.android.material.textfield.TextInputLayout
组件设置样式。
<style name="AppTheme" parent="Theme.MaterialComponents.Light">
....
<item name="textInputStyle">@style/MyTextInputLayoutStyle</item>
</style>
<style name=MyTextInputLayoutStyle" parent="@style/Widget.MaterialComponents.TextInputLayout.FilledBox">
....
</style>
如果您想自定义 TextInputEditText
,只需添加:
<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>
例如:
<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>
答案 1 :(得分:0)
新的MDC组件在此处具有很大的灵活性,但仍然很奇怪。我想要所有TextInputEditTexts的自定义字体,以及填充(默认)样式。对我来说唯一可行的方法是使用全局样式属性在TextInputEditText上指定字体:
<!-- 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"/>
例如,如果您未指定editTextStyle
,则textInputStyle
可以正常工作,但是却缺少填充。无论如何,我建议同时使用这两个字段并根据需要设置它们的样式。