将主题色应用于编辑文本



是否可以将主题色应用于特定的编辑文本,而其他主题色不受影响?

我有这个:

<?xml version="1.0" encoding="utf-8"?>
<resources>
  <!-- EditText on a blue background -->
  <style name="BlueBGTextView" parent="@android:style/Widget.Material.Light.EditText">
  </style>
  <!-- default EditText -->
  <style name="AppTheme.EditTextStyle" parent="@android:style/Widget.Material.Light.EditText">
  </style>
  <!-- default Theme -->
  <style name="AppTheme" parent="@android:style/Theme.Material.Light.NoActionBar"> 
    <item name="android:editTextStyle">@style/AppTheme.EditTextStyle</item>
    <item name="android:colorAccent">#ff0288d1</item> <!-- use to setup the color of the text selection handles -->   
    <item name="@attr/BlueBGTextViewStyle">@style/BlueBGTextView</item>
  </style>
</resources>

但问题是<item name="android:colorAccent">#ff0288d1</item>全局应用于我的所有编辑。我只想将其应用于

<style name="BlueBGTextView" parent="@android:style/Widget.Material.Light.EditText">
</style>

知道我该怎么做吗?

我用过这样的希望这可以帮助你

<style name="MyEditTextTheme">
    <!-- Used for the bottom line when not selected / focused -->
    <item name="colorControlNormal">#9e9e9e</item>
    <!-- colorControlActivated & colorControlHighlight use the colorAccent color by default -->
</style>

现在,您可以将此主题应用于特定的编辑文本

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

试试这个,我已经在我的系统上测试过了。

 <EditText
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:theme="@style/Edittext" />

声明您的主题是样式.xml

<style name="Edittext" parent="Theme.AppCompat.Light">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">#ed2727</item>
    <item name="colorPrimaryDark">#ed2727</item>
    <item name="colorAccent">#ed2727</item>
    <item name="colorControlNormal">#ed2727</item>
    <item name="colorControlActivated">#ed2727</item>
    <item name="colorControlHighlight">#ed2727</item>
</style>

参考这里

最新更新