首页 文章

EditText的两种颜色类型

提问于
浏览
1

我试图为我的EditText获得两种不同的颜色类型 . 一个应该是“主要”而另一个应该是“重音”颜色 .

我将styles.xml文件编辑为:

<resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="colorAccent">@android:color/white</item>

        <!-- colorPrimary is used for the default action bar background -->
        <item name="colorPrimary">@color/primaryDark</item>

        <!-- colorPrimaryDark is used for the status bar -->
        <item name="colorPrimaryDark">@color/primaryDark</item>
    </style>

</resources>

但是我只获得了我的编辑句柄,并且cursorto是纯白色,并突出显示选择背景50%白色 . 只是强调色 . 这很好,看起来像这样:

但是,我还需要将高亮标记设置为主要颜色的50%,将光标设置为实心原色,并将处理设置为原始颜色 . 如此处所示,我无法完成此操作,并且光标和编辑句柄是“白色”,因此它们是不可见的:

无论如何得到两个主题的EditTexts?

1 回答

  • 1

    在EditText上使用 android:theme

    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
    
        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:theme="@style/EditTextTheme1"/>
    
        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:theme="@style/EditTextTheme2"/>
    </LinearLayout>
    

    和你的自定义EditText主题:

    <style name="EditTextTheme1" parent="ThemeOverlay.AppCompat.Light">
        <item name="colorPrimary">@color/cardview_dark_background</item>
        <item name="colorPrimaryDark">@color/colorPrimary</item>
        <item name="colorAccent">@color/colorAccent1</item>
      </style>
    
      <style name="EditTextTheme2" parent="ThemeOverlay.AppCompat.Dark">
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorAccent</item>
        <item name="colorAccent">@color/colorAccent2</item>
      </style>
    

相关问题