



下載本文檔
版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領
文檔簡介
第AndroidButton按鈕點擊背景和文字變化操作Android原生的按鈕點擊狀態是有變化的,但是如果是自己加了一個.png格式的圖片為背景色,按鈕點擊就不會有任何效果,為了達到點擊按鈕有一閃的效果,我們就需要準備兩張圖進行切換,而且文字也要變色,老規矩廢話不多說直接上代碼:
按鈕背景圖片放在drawable/background_button.xml
xmlversion=1.0encoding=utf-8
selectorxmlns:android=/apk/res/android
itemandroid:drawable=@drawable/bg_pressandroid:state_pressed=true/
itemandroid:drawable=@drawable/bg_normalandroid:state_enabled=true/
itemandroid:drawable=@drawable/bg_normal/
/selector
準備兩張圖片一張為bg_press.png,一張為bg_normal.png。
在需要變化的按鈕中設置:
Button
android:layout_width=wrap_content
android:layout_height=wrap_content
android:layout_alignParentRight=true
android:layout_centerVertical=true
android:layout_marginRight=50dp
android:focusable=false
android:gravity=center
android:textSize=24px
android:text=@string/str_tethering_modify
android:background=@drawable/background_button/
這有背景色變化就解決完了,下面到按鈕上的文字了,現在點擊按鈕按鈕上的文字是沒有變化的,為了達到按鈕文字顏色的變化我們再新建一個xml文件。
按鈕顏色變化drawable/button_color.xml
xmlversion=1.0encoding=utf-8
selectorxmlns:android=/apk/res/android
itemandroid:state_pressed=trueandroid:color=#975508/
itemandroid:state_focused=falseandroid:state_pressed=falseandroid:color=#E5960E/
itemandroid:state_focused=trueandroid:color=#975508/
itemandroid:state_focused=falseandroid:color=#E5960E/
/selector
加入到我們的按鈕textColor中
Button
android:layout_width=wrap_content
android:layout_height=wrap_content
android:layout_alignParentRight=true
android:layout_centerVertical=true
android:layout_marginRight=50dp
android:focusable=false
android:gravity=center
android:textSize=24px
android:textColor=@drawable/button_color
android:text=@string/str_tethering_modify
android:background=@drawable/background_button/
這樣直接使用背景和文字就都有點擊效果啦,但是如果有這樣一個需求,在某些條件下需要再設置按鈕文字的顏色button.setTextColor(color),這樣設置完后,發現我們按鈕上文字點擊又沒有變化了,我之前試著直接button.setTextColor(R.drawable.button_color);發現這樣是沒有任何用處的。這樣就需要使用ColorStateList來解決,顧名思義,就是定義顏色的狀態列表,通過監聽按鈕不同狀態來設置不同的顏色,
老規矩,廢話不多說了,直接貼代碼:
*按鈕點擊顏色變化
privateColorStateListcolorStateList;
colorStateList=(ColorStateList)getResources().getColorStateList(R.drawable.button_color);
if(xxx){
button.setTextColor(Color.White);
}else{
button.setTextColor(colorStateList);
}
這樣就完美解決了按鈕點擊狀態的變化啦。
補充知識:androidstudio設置按鈕和背景融為一體也就是按鈕去除陰影
Button
android:layout_width=wrap_content
android:layout_height=wrap_con
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經權益所有人同意不得將文件中的內容挪作商業或盈利用途。
- 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
- 6. 下載文件中如有侵權或不適當內容,請與我們聯系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 港口碼頭建設安全施工方案及措施
- 汽車制造項目經理部各崗位職責他
- JL1901年度供應鏈管理內部審核計劃
- 熱力管網施工安全文明施工措施
- 三年級上冊地方課程教學計劃推進他
- 2025年度幼兒園團支部團隊建設計劃
- 物流運輸警示教育懺悔錄心得體會
- 護理質量安全事件調查職責
- 教師信息技術教學方法創新計劃
- 期貨模擬交易心得體會及技術應用
- 燃料電池行業發展分析及投資前景預測研究報告2025-2028版
- 2025年 珠海市市直專職人民調解員招聘筆試考試試卷附答案
- 2025年 物業管理師三級考試練習試題附答案
- 肺動脈高壓講課件
- 2024年滁州市機電工程學校招聘筆試真題
- 2025至2030中國大蔥產品行業市場發展現狀及發展趨勢與投資報告
- 絲織品微生物防治-洞察及研究
- 2025江蘇農牧科技職業學院工作人員招聘考試真題
- 2025至2030年中國生產工業控制類軟件行業市場運行格局及戰略咨詢研究報告
- 九師聯盟2024-2025學年高二下學期6月摸底聯考語文試題(含答案)
- 公司設備設施管理制度
評論
0/150
提交評論