1. 程式人生 > >點選或者按下背景或字型顏色修改使用及注意問題

點選或者按下背景或字型顏色修改使用及注意問題

用過selector的都知道,當我們為了更好使用者體驗,當點選button 或者觸控字型時 ,顏色需要出現相應變化,使用selector背景選擇器。
怎麼使用呢?簡單介紹下。
如果作為佈局或者控制元件背景時,在res下建立drawable資料夾,在drawable資料夾下,新建一個XXX.xml的檔案,出現
如下:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

</selector>

selector中要寫程式碼(各種狀態的item)

<item android:drawable="@drawable/press" android:state_focused="true" />
    <item android:drawable="@drawable/press" android:state_selected="true" />
    <item android:drawable="@drawable/press" android:state_pressed="true" />
    <item android:drawable="@drawable/normal" />

解釋狀態:

 android:color="mcolor" //正常情況下
        android:state_pressed=["true" | "false"]//是否觸控 
        android:state_focused=["true" | "false"]//是否獲得焦點
        android:state_selected=["true" | "false"]//是否被選狀態
        android:state_checkable=["true" | "false"]//是否可選
        android:state_checked=["true" | "false"]//是否選中
android:state_enabled=["true" | "false"]//是否可用 android:state_window_focused=["true" | "false"] />//是否視窗聚焦

如果我們想要文字顏色改變,那麼我們需要在res下建立color資料夾,在color資料夾下新建xxx.xml,selector使用同drawable下用法。
demo:

點選佈局修改背景和其中文字

我們使用顏色,所以在res/color/bg.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:color="@color/colorGray" android:state_focused="true" />
    <item android:color="@color/colorGray" android:state_selected="true" />
    <item android:color="@color/colorGray" android:state_pressed="true" />
    <item android:color="@color/colorWhite" />

</selector>

如果使用圖片背景的話,使用drawable資料夾item寫法:

<item android:drawable="@drawable/graypic" android:state_selected="true" />

千萬不要在使用android:color=…
color中也不要使用drawable=…

最後在佈局及TextView檔案中使用即可

android:background="@color/bg"
android:textColor="@color/tvbg"

相關推薦

或者背景字型顏色修改使用注意問題

用過selector的都知道,當我們為了更好使用者體驗,當點選button 或者觸控字型時 ,顏色需要出現相應變化,使用selector背景選擇器。 怎麼使用呢?簡單介紹下。 如果作為佈局或者控制元件背景時,在res下建立drawable資料夾,在drawabl

Unity3D---UGUI---Button相關、、擡起、長事件

一、先介紹一下Inspector面板裡的東西 Button裡一共有兩個指令碼。 image.png Image(Script) 這個跟Image裡是一樣的 Button(Script) Interactable(互動):Button是否可以點

android實現按鈕圓角背景字型顏色都改變

實現圓角按鈕,點選的時候背景和字型的顏色都改變,這裡的實現效果如下:原狀態背景為白色,字型為藍色,當點選的時候背景為藍色,字型為白色。介紹兩種實現方式。1、使用button。2、實現自定義TextView。 1.使用Button首先在drawable下建一個Button

android 給button新增 不同狀態【 釋放】的樣式|異常報錯解決

步驟: 1.在drawable下新建兩個xml檔案,分別對不同狀態下的樣式進行定義。 2.對檔案內容,即btn樣式進行定義 <?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http:/

移動端開發H5頁面按鈕後出現閃爍黑色背景的解決辦法

-webkit-tap-highlight-color:transparent; *{-webkit-tap-highlight-color:transparent; } H5頁面在IOS端測試的時候

LinearLayout(pressed)獲取焦點(focused)時背景設定不同顏色圖片

如果您認為本部落格不錯,讀後覺得有收穫,不妨打賞讚助我一下,讓我有動力繼續寫出高質量的部落格。 贈人玫瑰,手有餘香。分享技術,傳遞快樂。 有心課堂,傳遞的不僅僅是技術! QQ交流群:250468947 有心課堂會員,請加入VIP QQ交流

設定TextView等View元件效果。修改背景字型顏色

Android TextView等 預設是不帶點選效果的。。有的時候為了給使用者一個提示。可能需要我們自己新增點選效果。很多APP都是這樣實現的。 下面上一張圖看看效果。 下面是程式碼。 <TextView android:id="@+id/tv" android

實現listview條目後改變item背景顏色字型顏色並保留

先上效果圖。 其實實現是很簡單的在item的佈局中設定背景顏色選擇器,當item被選中時就會改變背景顏色 字型顏色在listview的adapter這種進行設定,定義標記記錄當前被點選的item

Button背景字型顏色改變的實現方法

Button 點選背景顏色改變 Android Studio 中如何設定Button點選背景顏色改變 具體方法: 2. button 中字型顏色點選改變

【android】設定View字型變色和邊框背景設定

1.設定字型變色 selector_orange_white.xml <?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/a

input消除預設背景顏色

在谷歌瀏覽器會出現預設點選input框黃色背景,如何去除? //消除google瀏覽器黃色框 input:-webkit-autofill, input:-webkit-autofill:hover, input:-webkit-autofill:focus { box-shadow:0 0 0 60

音樂網站開發:實現按鈕切換頁面背景圖的功能

        最近這一星期在做一個簡單小型的音樂播放器網站,目前各種功能基本都已經實現,包括切換上一曲下一曲,播放與暫停,隨機播放單曲迴圈順序播放模式切換,一首播放完畢自動按模式切換至下一曲,載入單句歌詞及所有歌詞等功能。另外就是本篇部落格要介紹的功能了,點

原生JS實現一個按鈕顯示一個div,再按鈕div隱藏,除div外其它空白處div隱藏

<!DOCTYPE html> <html style="font-size: 24px"> <head> <title>js點選按鈕顯示再點選空白地方隱藏</title> <style type="text/css"&g

jq實現跟隨滑鼠移動的劃線效果

效果如下: 1.html程式碼:   1 <div class="center-left-tap"> 2 <a href="javascript:void (0)" style="color: #1c1c1c;" class="current" oncli

RecyclerView的--長--以及item裡面控制元件的事件

主要是在介面卡裡面編寫在程式碼裡面呼叫: 核心程式碼: private OnItemClickListener onItemClickListener;//點選事件 private OnItemLongClickListener onItemLongClickListener

Android ,MVP+retrofit +rxjava+glide recyclerview使用詳解 ,條目,三種管理器 ,分割線

首先是對應的依賴  implementation 'com.android.support:recyclerview-v7:26.1.0' 下面是對應的介面卡 裡面對應的 有點選的註釋 public class HomeAdaper extends RecyclerV

獲取li標的幾種方式

應用閉包時回溯了下幾種關於獲取li下標的方式   1.點選獲取li下標 ———— 閉包   2.點選獲取li下標 ———— es6  let   3.點選獲取li下標 ———— 給元素新增下標屬性index    

關於實現一行,一行變色問題

今天在做如題目說的效果(如下程式碼)時,發現在時間註冊函式中判斷迴圈值出現問題。所有的節點的點選事件後都是第一行高亮。 <!doctype html> <html lang="en"> <head> <meta charset

react使用原生js獲取的ul的第幾個li 並設定className為active

import React from 'react' import './index.less'; import { Link } from "react-router-dom"; class Index extends React.Component { state

MFC對話方塊修改背景顏色新增背景圖片、控制元件背景字型顏色修改

MFC對話方塊修改背景顏色: 在CXXXDlg::OnInitDialog()中新增: CDialogEx::SetBackgroundColor(RGB(155,170,190)); 效果如