【问题标题】:Android | Change theme of TextInputLayout using styles.xml安卓 |使用 styles.xml 更改 TextInputLayout 的主题
【发布时间】:2017-02-09 14:14:31
【问题描述】:

我想在一个主题中将自定义样式应用于TextInputLayout's hinterror 并全局应用它,即在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>

我们可以像这样处理Button 小部件:

styles.xml

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="buttonStyle">@style/my.Widget.Button</item>
    <item name="android:buttonStyle">@style/my.Widget.Button</item>
</style>

<style name="my.Widget.Button" parent="@android:style/Widget.TextView">
    <item name="android:gravity">center</item>
    <item name="android:textSize">@dimen/some_dimen</item>
    <item name="android:textAllCaps">false</item>
</style>

注意:我目前正在考虑TextInputLayout 子类化作为最后的手段,所以请在回答时记住这一点。

谢谢:)

【问题讨论】:

    标签: android xml android-styles android-textinputlayout


    【解决方案1】:

    不幸的是,TextInputLayout 小部件,似乎设计支持库中的所有小部件都没有为其默认样式定义全局主题属性。因此,无法全局自定义其样式,只能通过子类化来支持自定义主题属性来查询默认样式,并在任何地方使用子类。

    请注意,Widget.Design.TextInputLayout 样式仍将被硬编码为默认样式,如果无法找到在其主题中定义的属性,小部件将回退到该样式,因此这与现有实现没有什么不同默认。

    似乎在设计支持库开发人员中存在一个误解,即定义默认主题属性需要它存在于当前主题中才能正常工作。这个问题以前是reported 用于TabLayout,但基于此推理已关闭,随后的查询和澄清没有产生进一步的响应。随意在 AOSP 问题跟踪器上打开另一张票并进行必要的说明;希望它可能会更好。

    【讨论】:

      【解决方案2】:

      我在项目中使用的解决方案:

      <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>
      

      【讨论】:

      • 这应该是公认的答案
      【解决方案3】:

      我确实在用这个

      <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>
      

      【讨论】:

        【解决方案4】:

        只是在这里大声思考,您尝试过吗?这应该会改变整个应用程序的颜色提示,但如果你想要这个,这可能会对你的情况有所帮助。

        <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
            <!-- Customize your theme here. -->
            <item name="colorPrimary">@color/colorPrimary</item>
            <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
            <item name="colorAccent">@color/colorAccent</item>
            <item name="android:textColorHint">@style/text_hint_appearance</item>
        </style>
        
        <style name="text_hint_appearance" parent="@android:style/TextAppearance">
            <item name="android:textColorHint">@color/success_accent</item>
        </style>
        

        【讨论】:

        • android:textColorHint 是样式属性,不是主题属性,所以不能在主题中定义。您的意思是改用android:textAppearance 主题属性吗?请注意,这也不起作用,因为TextInputLayout 实际上是一个包装类,它扩展了LinearLayout 而不是EditText,它提供了自己的提示绘制机制,它不考虑默认文本外观。
        猜你喜欢
        • 1970-01-01
        • 2019-07-30
        • 1970-01-01
        • 1970-01-01
        • 2019-04-27
        • 2010-12-18
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多