Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
您现在的位置: Android教程網 >> Android系統教程 >  >> Android 技術 >> Android 開發實例

Android 自訂 EditText 與 AutoCompleteTextView 的文字選取底色

編輯:Android 開發實例

多數 Android ROM 上,文字被選取時的背景色是清爽的鮮綠色,視覺上不難看。但某些三星機型卻是藍色,和整體介面放在一起格外突兀。好消息是,這個顏色完全可以透過程式自訂。

用到的屬性

android:textColorHighlight — Color of the text selection highlight.

顧名思義,它控制的正是選取範圍的高亮底色。

EditText 的效果

AutoCompleteTextView 的效果

實作程式碼

直接在 XML 中加上 android:textColorHighlight 即可,兩種控件寫法完全一樣:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    <EditText
        android:id="@+id/et_inputBox"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:textColorHighlight="#B4DF87"
        />

    <AutoCompleteTextView
        android:id="@+id/act_input"
        android:layout_below="@id/et_inputBox"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textColorHighlight="#B4DF87"
        />
</LinearLayout>

若希望在樣式中統一管理,也可以把這個屬性收進自訂 style,讓全 App 的選取色保持一致,不必逐頁設定。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved