How to change the floating label color of TextInputLayout

前端 未结 25 1754
[愿得一人]
[愿得一人] 2020-11-22 15:03

With reference to the new TextInputLayout released by Google, how do I change the floating label text color?

Setting colorControlNormal,

相关标签:
25条回答
  • 2020-11-22 15:31

    This is simple but the developer gets confused due to multiple views having the same attributes in different configurations/namespaces.

    In the case of the TextInputLayout we have every time a different view and with params either with TextInputEditText or directly to TextInputLayout.

    I was using all the above fixes: But I found that I was using

                    app:textColorHint="@color/textcolor_black"
    

    actually i should be using

                    android:textColorHint="@color/textcolor_black"
    

    As an attribute of TextinputLayout

    textcolor_black.xml

    <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:color="@color/black_txt" android:state_enabled="true" />
        <item android:color="@color/black_txt" android:state_selected="true" />
        <item android:color="@color/txtColorGray" android:state_selected="false" />
        <item android:color="@color/txtColorGray" android:state_enabled="false" />
    </selector>
    
    0 讨论(0)
  • 2020-11-22 15:32

    I suggest you make style theme for TextInputLayout and change only accent color. Set parent to your app base theme:

     <style name="MyTextInputLayout" parent="MyAppThemeBase">
         <item name="colorAccent">@color/colorPrimary</item>
     </style>
    
     <android.support.design.widget.TextInputLayout
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"
      android:theme="@style/MyTextInputLayout">
    
    0 讨论(0)
  • 2020-11-22 15:34

    Because you must add colorControlNormal, colorControlActivated, colorControlHighLight items to main application theme:

    <!-- Base application theme. -->
        <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
    
            <item name="colorPrimary">@color/colorPrimary</item>
            <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
            <item name="colorAccent">@color/colorAccent</item>
    
            <item name="colorControlActivated">@color/yellow_bright</item>
            <item name="colorControlNormal">@color/yellow_black</item>
    
        </style>
    
    0 讨论(0)
  • 2020-11-22 15:35

    In the latest version of the support library (23.0.0+), TextInputLayout takes the following attribute in XML to edit the floating label color: android:textColorHint="@color/white"

    0 讨论(0)
  • 2020-11-22 15:35

    I solve the problem. This is Layout:

     <android.support.design.widget.TextInputLayout
               android:id="@+id/til_username"
               android:layout_width="match_parent"
               android:layout_height="wrap_content"
               android:hint="@string/username"
               >
    
               <android.support.v7.widget.AppCompatEditText android:id="@+id/et_username"
                   android:layout_width="match_parent"
                   android:layout_height="wrap_content"
                   android:singleLine="true"
                   />
           </android.support.design.widget.TextInputLayout>
    

    This is Style:

    <style name="AppBaseTheme" parent="Theme.AppCompat.Light.DarkActionBar">
            <!--
                Theme customizations available in newer API levels can go in
                res/values-vXX/styles.xml, while customizations related to
                backward-compatibility can go here.
            -->
        </style>
    <!-- Application theme. -->
    
    
     <style name="AppTheme" parent="AppBaseTheme">
            <!-- All customizations that are NOT specific to a particular API-level can go here. -->
            <item name="colorAccent">@color/pink</item>
            <item name="colorControlNormal">@color/purple</item>
            <item name="colorControlActivated">@color/yellow</item>
        </style>
    

    You should use your theme in application:

    <application
            android:allowBackup="true"
            android:icon="@drawable/ic_launcher"
            android:label="@string/app_name"
            android:theme="@style/AppTheme" >
    </application>
    
    0 讨论(0)
  • 2020-11-22 15:36

    Instead of Brahmam Yamani answer I prefer to use Widget.Design.TextInputLayout as parent. That ensures, that all required items are present, even if not all items are overwritten. In Yamanis answer, the app will crash with an unresolvable resource, if setErrorEnabled(true) is called.

    Simply change the style to the following:

    <style name="TextLabel" parent="Widget.Design.TextInputLayout">
        <!-- Hint color and label color in FALSE state -->
        <item name="android:textColorHint">@color/Color Name</item> 
        <item name="android:textSize">20sp</item>
        <!-- Label color in TRUE state and bar color FALSE and TRUE State -->
        <item name="colorAccent">@color/Color Name</item>
        <item name="colorControlNormal">@color/Color Name</item>
        <item name="colorControlActivated">@color/Color Name</item>
     </style>
    
    0 讨论(0)
提交回复
热议问题