Android |使用styles.xml更改TextInputLayout的主题
我想将自定义样式应用于一个主题中的Android |使用styles.xml更改TextInputLayout的主题,android,xml,android-styles,android-textinputlayout,Android,Xml,Android Styles,Android Textinputlayout,我想将自定义样式应用于一个主题中的TextInputLayout的hint和error,并全局应用,即在styles.xml中定义它,并以某种方式将其应用于整个应用程序中使用的所有TextInputLayouts,而无需添加内联,如下所示: <android.support.design.widget.TextInputLayout android:id="@+id/usernameWrapper" android:layout_width="match_parent"
TextInputLayout的hint
和error
,并全局应用,即在styles.xml中定义它,并以某种方式将其应用于整个应用程序中使用的所有TextInputLayouts
,而无需添加内联,如下所示:
<android.support.design.widget.TextInputLayout
android:id="@+id/usernameWrapper"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/email_username"
android:textColorHint="@color/grey_text"
app:theme="@style/my_custom_style">
<style name="my_custom_style" parent="Widget.Design.TextInputLayout">
<item name="android:textColorHint">@color/success_accent</item>
<item name="android:textSize">20sp</item>
</style>
注意:我目前正在考虑将TextInputLayout
子类化,作为最后的手段,因此,在回答时请记住这一点
谢谢:)
在这里大声思考,你试过这个吗?这应该会改变整个应用程序的颜色提示,但如果你想这样做,这可能会对你的情况有所帮助
@颜色/原色
@颜色/原色暗
@颜色/颜色重音
@样式/文本\u提示\u外观
@颜色/成功口音
不幸的是,TextInputLayout
小部件,以及设计支持库中的所有小部件,没有为其默认样式定义全局主题属性。因此,除了将其子类化以支持从中查询默认样式的自定义主题属性,并在任何地方使用子类之外,不可能全局自定义其样式
请注意,Widget.Design.TextInputLayout
样式仍将被硬编码为默认样式,如果小部件找不到在其主题中定义的属性,它将返回到默认样式,因此这与默认情况下的现有实现没有什么不同
设计支持库开发人员似乎有一种误解,即定义默认主题属性需要它出现在当前主题中才能正常工作。这个问题以前是针对TabLayout
的,但基于这个推理而结束,随后的查询和澄清没有产生进一步的响应。可以在AOSP问题跟踪程序上打开另一张票据,并进行必要的澄清;希望情况会好一些。实际上我在用这个
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
[...]// other definitions
<item name="editTextStyle">@style/EditTextDefault</item>
</style>
<style name="Widget.Design.TextInputLayout" parent="Widget.MaterialComponents.TextInputLayout.OutlinedBox.Dense">
<item name="hintTextAppearance">@style/AppTheme.TextFloatLabelAppearance</item>
<item name="errorTextAppearance">@style/AppTheme.TextErrorAppearance</item>
</style>
<style name="AppTheme.TextFloatLabelAppearance" parent="TextAppearance.Design.Hint">
<item name="android:textColor">@color/colorAccent</item>
</style>
<style name="AppTheme.TextErrorAppearance" parent="TextAppearance.Design.Error">
<item name="android:textColor">#d32f2f</item>
</style>
<style name="EditTextDefault" parent="android:Widget.EditText">
<item name="android:paddingLeft">10dp</item>
</style>
[…]//其他定义
@样式/编辑文本默认值
@style/AppTheme.TextFloatLabelAppearance
@style/AppTheme.text错误外观
@颜色/颜色重音
#d32f2f
10dp
我在项目中使用的解决方案:
<style name="AppBaseTheme" parent="Theme.MaterialComponents.DayNight.NoActionBar">
<item name="textInputStyle">@style/TextInputLayoutTheme</item>
</style>
@样式/文本输入布局模式
@android:彩色/透明
android:textColorHint
是一个样式属性,而不是主题属性,因此不能在主题中定义它。您是否打算改用android:textAppearance
主题属性?请注意,这也行不通,因为TextInputLayout
实际上是一个包装类,它扩展了LinearLayout
,而不是EditText
,并且它提供了自己的提示绘制机制,不考虑默认的文本外观。
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
[...]// other definitions
<item name="editTextStyle">@style/EditTextDefault</item>
</style>
<style name="Widget.Design.TextInputLayout" parent="Widget.MaterialComponents.TextInputLayout.OutlinedBox.Dense">
<item name="hintTextAppearance">@style/AppTheme.TextFloatLabelAppearance</item>
<item name="errorTextAppearance">@style/AppTheme.TextErrorAppearance</item>
</style>
<style name="AppTheme.TextFloatLabelAppearance" parent="TextAppearance.Design.Hint">
<item name="android:textColor">@color/colorAccent</item>
</style>
<style name="AppTheme.TextErrorAppearance" parent="TextAppearance.Design.Error">
<item name="android:textColor">#d32f2f</item>
</style>
<style name="EditTextDefault" parent="android:Widget.EditText">
<item name="android:paddingLeft">10dp</item>
</style>
<style name="AppBaseTheme" parent="Theme.MaterialComponents.DayNight.NoActionBar">
<item name="textInputStyle">@style/TextInputLayoutTheme</item>
</style>
<style name="TextInputLayoutTheme" parent="Widget.Design.TextInputLayout">
<item name="colorOnSurface">@android:color/transparent</item>
</style>