无法在样式中设置 colorControlNormal 和 android:textColor

Cannot set colorControlNormal and android:textColor in style

我正在尝试设置 material EditText 视图的样式:

<style name="AppTheme.EditText" parent="@style/Widget.AppCompat.EditText">
    <item name="android:textColor">@color/white</item>
    <item name="android:textColorHint">#8AFFFFFF</item>

    <item name="colorControlNormal">@color/white</item>
    <item name="colorControlActivated">@color/white</item>
    <item name="colorControlHighlight">@color/white</item>
</style>

然后我将样式应用到我的主题上:

<style name="AppTheme">
    <item name="editTextStyle">@style/AppTheme.EditText</item>
</style>

并将主题应用到 activity:

  <activity
        android:name=".MyActivity"
        android:configChanges="keyboardHidden|orientation|screenSize"
        android:windowSoftInputMode="stateAlwaysHidden"
        android:theme="@style/AppTheme">

然而,这并没有改变下划线颜色。

我知道我可以更改 accentColor 来更改下划线颜色,但我不想这样做,因为我需要我的强调色与其他一些控件不同。

我可以像这样设置控件下划线颜色的样式吗?

您可以通过执行以下操作更改下划线的颜色:

<style name="AppTheme.EditText" parent="@style/Widget.AppCompat.EditText">
    <item name="android:textColor">@color/white</item>
    <item name="android:textColorHint">#8AFFFFFF</item>

    <item name="android:backgroundTint">@color/white</item>
    <item name="backgroundTint">@color/white</item>
</style>

您确实应该将带有 android 命名空间的 item 放在它自己的 styles-v21.xml 文件中以避免 Android Studio 警告,但这不是 100% 必要。我在 API 19 模拟器中对此进行了测试,它工作正常,没有崩溃。

这个解决方案对我有用。 sIt 仅包括覆盖 edittext 样式主题中 colorControlActivatedcolorControlHighlightcolorControlNormal 的值。另一种想法是,如果您还想更改光标颜色,则 android:textCursorDrawable 使用此 属性。我把这个 edittext 样式放在 style.xml(v21) also.Then 中,想用这个主题做任何你想要的 activity。下面是一个例子:

First Solution

<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>
        <!--Edittext theme -->
        <item name="editTextStyle">@style/App_EditTextStyle</item>
</style>

<style name="App_EditTextStyle" parent="@style/Widget.AppCompat.EditText">
    <item name="android:textColor">@color/white</item>
    <item name="android:textColorHint">@color/black</item>

    <item name="colorControlNormal">@color/white</item>
    <item name="colorControlActivated">@color/white</item>
    <item name="colorControlHighlight">@color/white</item>

    <item name="android:backgroundTint" >@color/white</item>
    <item name="backgroundTint">@color/white</item>

    <item name="android:textCursorDrawable">@color/white</item>
</style>

Other Solution

您也可以通过编程方式进行设置。这是 API < 21 及以上

的解决方案
Drawable drawable = yourEditText.getBackground(); // get current EditText drawable 
    drawable.setColorFilter(Color.GREEN, PorterDuff.Mode.SRC_ATOP); // change the drawable color

    if(Build.VERSION.SDK_INT > 16) {
        yourEditText.setBackground(drawable); // set the new drawable to EditText
    }else{
        yourEditText.setBackgroundDrawable(drawable); // use setBackgroundDrawable because setBackground required API 16
    }

在这里,您只需按照以下步骤操作即可。

第 1 步:创建样式

android:textCursorDrawable 属性设置为 @null 应该会导致使用 android:textColor 作为光标颜色。

<style name="editTextTheme" parent="@style/Widget.AppCompat.EditText">

        <item name="android:textColorHint">@color/white</item>

        <item name="colorControlNormal">@color/white</item>
        <item name="colorControlActivated">@color/white</item>
        <item name="colorControlHighlight">@color/white</item>

        <item name="colorAccent">@color/white</item>
        <item name="android:textCursorDrawable">@null</item>
        <item name="android:textColorPrimary">@color/white</item>

    </style>

第 2 步:使用该样式作为主题

<style name="MyTheme">
        <item name="theme">@style/editTextTheme</item>
</style>

第 3 步:在您的组件中实施该主题

<android.support.v7.widget.AppCompatEditText
        android:id="@+id/editText"
        style="@style/MyTheme"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:hint="Enter Name"
        android:inputType="textPersonName" />

你的style.xml:

<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="editTextStyle">@style/MyEditTextStyle</item>
</style>

<style name="MyEditTextStyle" parent="@style/Widget.AppCompat.EditText">
    <item name="android:textColor">#FF0</item>
</style>

<style name="AppTheme.MyEditTextTheme">
    <item name="android:textColorHint">#8AFFFFFF</item>

    <item name="colorControlNormal">#FF0</item>
    <item name="colorControlActivated">#FF0</item>
    <item name="colorControlHighlight">#FF0</item>
</style>

然后应用 MyEditTextTheme 作为您的 EditText 的主题:

<EditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/AppTheme.MyEditTextTheme"/>

现在只有这个 EditText 有黄色下划线。您也可以在 MyEditTextTheme 中更改 colorAccent,它不会更改 AppTheme 的 colorAccent 值。

不工作的原因: colorControlNormaltheme 的属性,而 android:textColorstyle 属性。要更改 colorControlNormal,您必须覆盖 Theme,要更改 android:textColor 值,您必须覆盖 style . @style/Widget.AppCompat.EditText 或其任何父项中没有名为 colorControlNormal 的属性。因此,您在执行此操作时不会覆盖任何内容:

<style name="AppTheme.EditText" parent="@style/Widget.AppCompat.EditText">
    <item name="colorControlNormal">@color/white</item>
</style>

解法: 要实际更改 colorControlNormal 的值,您必须在主题中更改它。要在不更改 activity 主题的情况下做到这一点,您可以:

1/ 创建一个仅包含您希望更改的主题属性的 ThemeOverlay:

<style name="AppTheme.EditText"> 
    <item name="colorControlNormal">@color/white</item>
</style>

2/ 仅在您的 EditText 视图中使用它:

<EditText
    android:layout_width="match_parent"
    android:theme="@style/AppTheme.EditText"
/>

这里实际发生的是将使用视图的主题属性(如果存在)。 activity 的主题将用作后备。

您可以随时更改 activity 的主题 colorControlNormal 以影响整个 activity。