【问题标题】:Style custom TextInputLayout attribut from within theme从主题内设置自定义 TextInputLayout 属性
【发布时间】:2016-02-04 10:14:15
【问题描述】:

在设计库源码中我们可以找到这一行:

<declare-styleable name="TextInputLayout">
    <attr format="reference" name="hintTextAppearance"/>
    <attr name="android:hint"/>
    <attr format="boolean" name="errorEnabled"/>
    <attr format="reference" name="errorTextAppearance"/>
    <attr format="boolean" name="counterEnabled"/>
    <attr format="integer" name="counterMaxLength"/>
    <attr format="reference" name="counterTextAppearance"/>
    <attr format="reference" name="counterOverflowTextAppearance"/>
    <attr name="android:textColorHint"/>
    <attr format="boolean" name="hintAnimationEnabled"/>
</declare-styleable>

我想通过errorTextAppearance属性改变错误文本的颜色

我知道如何通过 TextInputLayout xml 声明中的 app:{atribut-name} 对其进行自定义,但我如何才能从我的主题定义中自定义其中一个属性?

【问题讨论】:

    标签: android android-theme android-support-design android-custom-attributes


    【解决方案1】:

    为了更简单,您有一个包含一组属性的自定义视图。要提供值,从底部(视图)到顶部(应用程序)有三种不同的方式

    1. 通过属性或以编程方式为视图提供值
    2. 创建不同的样式并使用style="@style/MyStyle"将它们分配给此视图
    3. 创建一个属性并将其分配给自定义视图的构造函数的defStyleAttr 参数。并在你的主题中为这个属性分配一个样式,瞧!!

    对于前两个,它是直截了当的。让我们专注于最后一种方法。有四个步骤可以实现这一目标

    第 1 步

    创建一个属性(一般在res/values/attrs.xml文件中

    <resources>
        <attr name="myCustomViewAttr" format="reference" />
    
        <!-- Below are your set of attributes in declare styleable -->
        <declare-styleable name="TextInputLayout">
           <attr format="reference" name="hintTextAppearance"/>
           <attr name="android:hint"/>
           <attr format="boolean" name="errorEnabled"/>
           <attr format="reference" name="errorTextAppearance"/>
           <attr format="boolean" name="counterEnabled"/>
           <attr format="integer" name="counterMaxLength"/>
           <attr format="reference" name="counterTextAppearance"/>
           <attr format="reference" name="counterOverflowTextAppearance"/>
           <attr name="android:textColorHint"/>
           <attr format="boolean" name="hintAnimationEnabled"/>
        </declare-styleable>
    </resources>
    

    第 2 步

    在自定义视图中使用这个新创建的属性。

    假设您的自定义视图的类名是MyCustomView。你的类的构造函数看起来像这样

    class MyCustomView @JvmOverloads constructor(
        context: Context,
        attrs: AttributeSet? = null,
        defStyleAttr: Int = R.attr.myCustomViewAttr
    ) : TextInputLayout(context, attrs, defStyleAttr)
    

    如果没有为布局中的属性提供任何内容,这将强制MyCustomView 使用来自myCustomViewAttr 的属性。

    第 3 步

    创建一个样式,这是最简单且被大多数人使用的样式

    <style name="Widget.MyApp.MyCustomView" parent="">
       <!-- Assuming you have already defined TextAppearance.MyApp.ErrorText as you required -->
       <item name="errorTextAppearance">@style/TextAppearance.MyApp.ErrorText</item>
    </style>
    

    如果您想为您的errorTextAppearance 分配一些其他属性,那么

    <style name="Widget.MyApp.MyCustomView" parent="">
       <!-- Assuming you have already defined TextAppearance.MyApp.ErrorText as you required -->
       <item name="errorTextAppearance">?attr/myAnotherDefinedAttribute</item>
    </style>
    

    第 4 步

    最后一步,将样式分配给属性

    <style name="Theme.MyApp" parent="Theme.MaterialComponents.Light">
       <item name="myCustomViewAttr">@style/Widget.MyApp.MyCustomView</style>
    </style>
    

    如果您的文本外观来自其他属性,那么您可以直接分配该属性

    <style name="Theme.MyApp" parent="Theme.MaterialComponents.Light">
       <item name="myCustomViewAttr">?attr/textAppearanceForError</style>
    </style>
    

    【讨论】:

      【解决方案2】:

      styles.xml:

      <style name="TextInputLayout.Error" parent="@style/TextAppearance.Design.Error">
          <item name="android:textColor">@color/textinput_error_color</item>
      </style>
      
      <style name="Theme.AppName.TextInputLayout" parent="@style/Widget.Design.TextInputLayout">
          <item name="errorTextAppearance">@style/TextInputLayout.Error</item>
      </style>
      

      在你的布局中:

      <android.support.design.widget.TextInputLayout
                      android:layout_width="match_parent"
                      android:layout_height="wrap_content"
                      style="@style/Theme.AppName.TextInputLayout">
      
                      ...
                  </android.support.design.widget.TextInputLayout>
      

      您还可以在Theme.AppName.TextInputLayout 中添加来自Widget.Design.TextInputLayout 的其他项目

      【讨论】:

      • 感谢您花时间回答。 :-) 当我在主题中写作时,我想知道如何使用主题概括的属性来应用这种风格。 (不直接对 TextInputLayout 应用样式或属性。)
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-12-17
      • 2019-07-17
      • 2021-07-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多