[英]How to change Android button click color
我有一個按鈕。 我已使用background屬性將背景圖像添加到我的按鈕中。 現在,當我單擊按鈕時,我沒有得到默認的橙色。 我怎么能得到那種顏色。
另一個查詢,在上述情況下,如何將默認的橙色更改為其他顏色。
您必須對按鈕進行自定義,以進行更改,以便在yoyr drawable文件夾中創建一個名為custom_btn的xml文件,並將代碼粘貼到其中,如下所示:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true"
android:drawable="@drawable/tick_btn" /> <!-- pressed -->
<item android:drawable="@drawable/untick_btn" /> <!-- default -->
</selector>
並在按鈕中添加android:button="@drawable/custom_btn"
// try this
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="hover color or drawable" android:state_pressed="true"></item>
<item android:drawable="hover color or drawable" android:state_focused="true"></item>
<item android:drawable="color or drawable" android:state_enabled="true" android:state_focused="false" android:state_pressed="false"></item>
<item android:drawable="hover color or drawable" android:state_enabled="false"></item>
</selector>
如果您只是想查看“默認橙色”,請使用ImageButton並將可繪制對象應用於src(而不是背景)。 然后,您將在圖像后面看到本機圖像背景,並且它將繼續做之前的工作。
我沒有獲得默認的橙色。 我怎么能得到那種顏色。
當您向按鈕添加背景時,它不再是默認的原始按鈕,它成為自定義按鈕,因為您已通過向其添加一些背景來呈現了默認行為。 現在,您需要自己為自定義按鈕添加顏色,因為操作系統僅處理未自定義的原始按鈕。
如何將默認的橙色更改為其他顏色。
按下按鈕后更改按鈕狀態的方法有兩種:
1)要么添加背景圖像。
2)或將xml添加到按鈕。
這是關於自定義Android按鈕的非常不錯的教程。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.