How to change the floating label color of TextInputLayout

前端 未结 25 1708
[愿得一人]
[愿得一人] 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:37

    how do I change the floating label text color?

    With the Material Components library you can customize the TextInputLayout the hint text color using (it requires the version 1.1.0)

    • In the layout:

    • app:hintTextColor attribute : the color of the label when it is collapsed and the text field is active

    • android:textColorHint attribute: the color of the label in all other text field states (such as resting and disabled)

    <com.google.android.material.textfield.TextInputLayout
         app:hintTextColor="@color/mycolor"
         android:textColorHint="@color/text_input_hint_selector"
         .../>
    
    • extending a material style Widget.MaterialComponents.TextInputLayout.*:
    <style name="MyFilledBox" parent="Widget.MaterialComponents.TextInputLayout.FilledBox">
        <item name="hintTextColor">@color/mycolor</item>
        <item name="android:textColorHint">@color/text_input_hint_selector</item>
    </style>
    

    The default selector for android:textColorHint is:

    <selector xmlns:android="http://schemas.android.com/apk/res/android">
      <item android:alpha="0.38" android:color="?attr/colorOnSurface" android:state_enabled="false"/>
      <item android:alpha="0.6" android:color="?attr/colorOnSurface"/>
    </selector>
    
    0 讨论(0)
  • 2020-11-22 15:41

    Try The Below Code It Works In Normal State

    <android.support.design.widget.TextInputLayout
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
     android:theme="@style/TextLabel">
    
     <android.support.v7.widget.AppCompatEditText
         android:layout_width="match_parent"
         android:layout_height="wrap_content"
         android:hint="Hiiiii"
         android:id="@+id/edit_id"/>
    
    
    </android.support.design.widget.TextInputLayout>
    

    In Styles Folder TextLabel Code

     <style name="TextLabel" parent="TextAppearance.AppCompat">
    <!-- 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)
  • 2020-11-22 15:42

    you should change your colour here

    <style name="Base.Theme.DesignDemo" parent="Theme.AppCompat.Light.NoActionBar">
            <item name="colorPrimary">#673AB7</item>
            <item name="colorPrimaryDark">#512DA8</item>
            <item name="colorAccent">#FF4081</item>
            <item name="android:windowBackground">@color/window_background</item>
        </style>
    
    0 讨论(0)
  • 2020-11-22 15:46

    Ok, so, I found this answer very helpful and thanks to all the people who contributed. Just to add something, though. The accepted answer is indeed the correct answer...BUT...in my case, I was looking to implement the error message below the EditText widget with app:errorEnabled="true" and this single line made my life difficult. it seems that this overrides the theme I chose for android.support.design.widget.TextInputLayout, which has a different text color defined by android:textColorPrimary.

    In the end I took to applying a text color directly to the EditText widget. My code looks something like this:

    styles.xml

    <item name="colorPrimary">@color/my_yellow</item>
    <item name="colorPrimaryDark">@color/my_yellow_dark</item>
    <item name="colorAccent">@color/my_yellow_dark</item>
    <item name="android:textColorPrimary">@android:color/white</item>
    <item name="android:textColorSecondary">@color/dark_gray</item>
    <item name="android:windowBackground">@color/light_gray</item>
    <item name="windowNoTitle">true</item>
    <item name="windowActionBar">false</item>
    <item name="android:textColorHint">@color/dark_gray</item>
    <item name="android:colorControlNormal">@android:color/black</item>
    <item name="android:colorControlActivated">@android:color/white</item>
    

    And my widget:

    <android.support.design.widget.TextInputLayout
            android:id="@+id/log_in_layout_name"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:errorEnabled="true">
    
            <EditText
                android:id="@+id/log_in_name"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal"
                android:textColor="@android:color/black"
                android:ems="10"
                android:hint="@string/log_in_name"
                android:inputType="textPersonName" />
    </android.support.design.widget.TextInputLayout>
    

    Now it displays black text color instead of the textColorPrimary white.

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

    Now, simply using colorAccent and colorPrimary will work perfectly.

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

    Found the answer, use android.support.design:hintTextAppearance attribute to set your own floating label appearance.

    Example:

    <android.support.design.widget.TextInputLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        app:hintTextAppearance="@style/TextAppearance.AppCompat">
    
        <EditText
            android:id="@+id/password"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="@string/prompt_password"/>
    </android.support.design.widget.TextInputLayout>
    
    0 讨论(0)
提交回复
热议问题