自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Android中focusable屬性的妙用之底層按鈕的實(shí)現(xiàn)

移動(dòng)開發(fā) Android
最近比較熱衷于Android開發(fā)這方面,關(guān)注到了許多像下面這樣對例子,分享一下。Focusable 是 Microsoft .NET 屬性訪問器,它實(shí)際上是一個(gè)依賴項(xiàng)屬性。 這一特定依賴項(xiàng)屬性非常普遍地在派生元素類(尤其是控件)中以不同方式設(shè)置其原本的“默認(rèn)”值。 這種情況通常以兩種方式發(fā)生

在Android中使用focusable 屬性來實(shí)現(xiàn)按鈕的特效,看到百威啤酒的客戶端主界面的按鈕,感覺比較新奇,先看下圖片:

注意圖中我畫的箭頭,當(dāng)時(shí)鼠標(biāo)點(diǎn)擊的黑色圈圈的位置,然后按鈕出現(xiàn)了按下的效果(黃色的描邊)

剛開始看到這種效果很是好奇,不知道是怎么實(shí)現(xiàn)的,后來仔細(xì)一想,應(yīng)該是整個(gè)啤酒罐是一張圖片(ImageView),該圖片是布局在三個(gè)按鈕之上,然后就是最關(guān)鍵的地方,把圖片設(shè)置為不可獲取焦點(diǎn),也就是android:focusable="false" ,就這樣簡單的一行,就可以搞定了!

為了驗(yàn)證我的想法,我建了一個(gè)工程來做測試,效果如下圖所示:

具體代碼如下:

main.xml:

  1. <?xml version="1.0" encoding="utf-8"?>   
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"   
  3.     android:layout_width="fill_parent"   
  4.     android:layout_height="fill_parent"   
  5.     >   
  6.     <LinearLayout   
  7.         android:layout_width="match_parent"   
  8.         android:layout_height="wrap_content"   
  9.         android:orientation="vertical" >   
  10.         <Button   
  11.             android:layout_width="match_parent"   
  12.             android:layout_height="wrap_content"   
  13.             android:layout_margin="10dp"   
  14.             android:text="button1"   
  15.             android:background="@drawable/button_selector"   
  16.             />      
  17.         <Button   
  18.             android:layout_width="match_parent"   
  19.             android:layout_height="wrap_content"   
  20.             android:layout_margin="10dp"   
  21.             android:text="button2"   
  22.             android:background="@drawable/button_selector"   
  23.             />    
  24.         <Button   
  25.             android:layout_width="match_parent"   
  26.             android:layout_height="wrap_content"   
  27.             android:layout_margin="10dp"   
  28.             android:text="button3"   
  29.             android:background="@drawable/button_selector"   
  30.             />    
  31.     </LinearLayout>   
  32.     <ImageView   
  33.         android:layout_width="wrap_content"   
  34.         android:layout_height="wrap_content"   
  35.         android:src="@drawable/bg2"   
  36.         android:focusable="false"   
  37.         />   
  38. </RelativeLayout> 

button_selector.xml:

  1. <?xml version="1.0" encoding="utf-8"?>   
  2. <selector   
  3.     xmlns:android="http://schemas.android.com/apk/res/android">   
  4.     <item android:state_pressed="true" >   
  5.         <shape>   
  6.             <!-- 實(shí)心,即填充 -->   
  7.             <solid android:color="#8470FF"/>   
  8.             <!-- 描邊 -->   
  9.             <stroke   
  10.                 android:width="2dp"   
  11.                 android:color="#FFFF00"/>   
  12.             <!-- 圓角 -->   
  13.             <corners   
  14.                 android:radius="5dp" />   
  15.             <padding   
  16.                 android:left="10dp"   
  17.                 android:top="10dp"   
  18.                 android:right="10dp"   
  19.                 android:bottom="10dp" />   
  20.         </shape>   
  21.     </item>   
  22.  
  23.     <item>         
  24.         <shape>   
  25.             <!-- 實(shí)心,即填充 -->   
  26.             <solid android:color="#8470FF"/>   
  27.             <corners   
  28.                 android:radius="5dp" />   
  29.             <padding   
  30.                 android:left="10dp"   
  31.                 android:top="10dp"   
  32.                 android:right="10dp"   
  33.                 android:bottom="10dp" />   
  34.         </shape>   
  35.     </item>   
  36. </selector> 

關(guān)于button_selector.xml中shape的使用有疑問的可以看我上次的文章:Android中shape的使用。

【編輯推薦】

Android ListView詳解

Android開發(fā)中插入新的Activity

在Android應(yīng)用程序中使用Internet數(shù)據(jù)

責(zé)任編輯:zhaolei 來源: 博客園
相關(guān)推薦

2010-09-10 15:16:51

CSSdisplay

2010-09-08 15:16:46

clearCSS

2010-09-09 16:54:05

CSSclear

2024-04-25 08:22:43

AndroidlargeHeap屬性

2010-08-30 16:02:06

CSSclear

2023-02-01 08:31:48

2016-03-03 15:11:42

Spark Strea工作流調(diào)度器

2010-09-03 10:18:06

CSSdisplay:inl

2022-12-26 09:27:48

Java底層monitor

2010-07-15 14:23:42

SQL Server數(shù)

2024-05-28 12:25:33

Pythonglobals?函數(shù)

2011-06-01 14:20:37

Android

2011-08-12 11:31:46

iPhoneUIView動(dòng)畫

2024-12-19 09:00:00

字典視圖對象Python

2021-06-09 11:41:10

RateLimiterJava代碼

2021-01-08 08:34:09

Synchronize線程開發(fā)技術(shù)

2009-06-03 09:01:41

微軟Windows 7操作系統(tǒng)

2021-10-20 07:36:03

Python構(gòu)造方法

2023-07-11 08:00:00

2013-04-16 14:42:38

云計(jì)算智能手機(jī)移動(dòng)通信網(wǎng)絡(luò)
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)