Android 自定義Button控件實(shí)現(xiàn)按鈕點(diǎn)擊變色
效果圖如下所示:

一、shape 樣式:(在drawable新建--》new--》Drawable resource file 在父級(jí)標(biāo)簽selector添加Item )
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_pressed="true"> <shape android:shape="rectangle"> <solid android:color="#73c4f3" /> <stroke android:width="2dp" android:color="#ffffff" /> <corners android:radius="20dp" /> <padding android:bottom="5dp" android:left="10dp" android:right="10dp" android:top="5dp" /> <gradient android:angle="270" android:endColor="#8accf2" android:startColor="#8accf2" android:type="sweep" /> </shape> <shape> <solid android:color="#3fb3f6" /> <stroke android:width="2dp" android:color="#ffffff" /> <corners android:radius="20dp" /> <padding android:bottom="5dp" android:left="10dp" android:right="10dp" android:top="5dp" /> </shape> </item> </selector>
二、style 樣式:
<style name="rectangle"> <item name="android:layout_width">match_parent</item> <item name="android:layout_height">wrap_content</item> <item name="android:textSize">15sp</item> <item name="android:textColor">#ffffff</item> <item name="android:background">@drawable/buttonclickstyle</item> </style>
三、Button控件調(diào)用style樣式:
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.example.ly.blogtest.MainActivity"> <Button android:id="@+id/button" style="@style/rectangle" android:text="@string/btn1" /> </RelativeLayout>
----------------------------------按鈕點(diǎn)擊變色-----------------------------------------------------
----------------------------------item屬性標(biāo)記------------------------------
android:state_pressed 是否按下,如一個(gè)按鈕觸摸或者點(diǎn)擊。
android:state_focused 是否取得焦點(diǎn),比如用戶選擇了一個(gè)文本框。
android:state_hovered 光標(biāo)是否懸停,通常與focused state相同,它是4.0的新特性
android:state_selected 被選中,它與focus state并不完全一樣,如一個(gè)list view 被選中的時(shí)候,它里面的各個(gè)子組件可能通過(guò)方向鍵,被選中了。
android:state_checkable 組件是否能被check。如:RadioButton是可以被check的。
android:state_checked 被checked了,如:一個(gè)RadioButton可以被check了。
android:state_enabled 能夠接受觸摸或者點(diǎn)擊事件
android:state_activated 被激活
android:state_window_focused
應(yīng)用程序是否在前臺(tái),當(dāng)有通知欄被拉下來(lái)或者一個(gè)對(duì)話框彈出的時(shí)候應(yīng)用程序就不在前臺(tái)了
注意:如果有多個(gè)item,那么程序?qū)⒆詣?dòng)從上到下進(jìn)行匹配,最先匹配的將得到應(yīng)用。(不是通過(guò)最佳匹配)
如果一個(gè)item沒有任何的狀態(tài)說(shuō)明,那么它將可以被任何一個(gè)狀態(tài)匹配。
----------------------------------item屬性標(biāo)記------------------------------
以上所述是小編給大家介紹的Android 自定義Button控件實(shí)現(xiàn)按鈕點(diǎn)擊變色,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
- Android開發(fā)設(shè)置RadioButton點(diǎn)擊效果的方法
- Android 點(diǎn)擊ImageButton時(shí)有“按下”的效果的實(shí)現(xiàn)
- Android懸浮按鈕點(diǎn)擊返回頂部FloatingActionButton
- Android Button按鈕的四種點(diǎn)擊事件
- Android開發(fā)-之監(jiān)聽button點(diǎn)擊事件的多種方法
- Android中button點(diǎn)擊后字體的變色效果
- Android自定義button點(diǎn)擊效果的兩種方式
- Android開發(fā)之創(chuàng)建可點(diǎn)擊的Button實(shí)現(xiàn)方法
- Android實(shí)現(xiàn)點(diǎn)擊Button產(chǎn)生水波紋效果
- Android Button點(diǎn)擊事件的四種實(shí)現(xiàn)方法
相關(guān)文章
詳解VirtualApk啟動(dòng)插件Activity
這篇文章主要介紹了詳解VirtualApk啟動(dòng)插件Activity,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-05-05
Android實(shí)現(xiàn)密碼明密文切換(小眼睛)
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)密碼明密文切換,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-08-08
Android App使用SQLite數(shù)據(jù)庫(kù)的一些要點(diǎn)總結(jié)
這篇文章主要介紹了Android App使用SQLite數(shù)據(jù)庫(kù)的一些要點(diǎn)總結(jié),使用Sqlite作為應(yīng)用的嵌入式數(shù)據(jù)庫(kù)非常輕便,需要的朋友可以參考下2016-03-03
Android開發(fā)中聽筒無(wú)法播放音樂的解決方法
這篇文章主要介紹了Android開發(fā)中聽筒無(wú)法播放音樂的解決方法,涉及Android權(quán)限控制中的相關(guān)屬性設(shè)置技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2016-10-10
Android開發(fā)實(shí)現(xiàn)的圖片瀏覽功能示例【放大圖片】
這篇文章主要介紹了Android開發(fā)實(shí)現(xiàn)的圖片瀏覽功能,結(jié)合實(shí)例形式分析了Android針對(duì)圖片的切換顯示、透明度、大小調(diào)整等相關(guān)操作技巧,需要的朋友可以參考下2019-04-04
Android逐幀動(dòng)畫實(shí)現(xiàn)代碼
這篇文章主要為大家詳細(xì)介紹了Android逐幀動(dòng)畫實(shí)現(xiàn)代碼,可以通過(guò)xml或java代碼實(shí)現(xiàn),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-01-01

