如何在TextInputLayout上设置计数器的颜色?

And*_*ndy 8 android android-design-library

我正在使用围绕EditText的TextInputLayout.现在柜台是黑色的,我希望它是白色的.我不确定设置什么选项让它变成白色.

    <android.support.design.widget.TextInputLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:counterEnabled="true"
        android:textColor="@color/white"
        android:textColorHint="@color/white"
        >

        <EditText
            android:id="@+id/myfield"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:backgroundTint="@color/white"
            android:hint="@string/myfield_hint"
            android:inputType="text"
            android:maxLength="26"
            android:textColor="@color/white"
            android:textColorHint="@color/white"/>
    </android.support.design.widget.TextInputLayout>
Run Code Online (Sandbox Code Playgroud)

小智 14

我决定根据之前的评论来写这个答案.

首先,您需要为计数器创建样式,例如:

<style name="CounterStyle" parent="TextAppearance.AppCompat.Small">
    <item name="android:textColor">@android:color/white</item>
    <!--other parameters that you want to change -->
</style>
Run Code Online (Sandbox Code Playgroud)

然后将此样式添加到TextInputLayout:

app:counterTextAppearance="@style/CounterStyle"
Run Code Online (Sandbox Code Playgroud)

这就是全部,它应该有效.完整代码:

<android.support.design.widget.TextInputLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:counterEnabled="true"
        app:counterTextAppearance="@style/CounterStyle"
        android:textColor="@color/white"
        android:textColorHint="@color/white">

        <EditText
            android:id="@+id/myfield"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:backgroundTint="@color/white"
            android:hint="@string/myfield_hint"
            android:inputType="text"
            android:maxLength="26"
            android:textColor="@color/white"
            android:textColorHint="@color/white"/>
    </android.support.design.widget.TextInputLayout>
Run Code Online (Sandbox Code Playgroud)

您还可以为溢出计数器使用某种样式:

app:counterOverflowTextAppearance="@style/YourOverflowTextStyleHere"
Run Code Online (Sandbox Code Playgroud)

如上所述这里(感谢@Sufian的链接)


Act*_*tty 4

请加:

app:counterTextAppearance="@android:color/white"
Run Code Online (Sandbox Code Playgroud)

到您的 TextInputLayout。

希望这可以帮助!

  • 实际上“counterTextAppearance”属性接收的不是颜色,而是样式。因此,创建如下样式:“&lt;style name="TextLabel"parent="TextAppearance.AppCompat"&gt;&lt;item name="android:textColor"&gt;@color/white&lt;/item&gt;&lt;/style&gt;”,然后设置 TextInputLayout 的属性: "app:counterTextAppearance="@style/TextLabel" (13认同)