Android中focusable属性的妙用之底层按钮的实现

移动开发 Android
最近比较热衷于Android开发这方面,关注到了许多像下面这样对例子,分享一下。Focusable 是 Microsoft .NET 属性访问器,它实际上是一个依赖项属性。 这一特定依赖项属性非常普遍地在派生元素类(尤其是控件)中以不同方式设置其原本的“默认”值。 这种情况通常以两种方式发生

在Android中使用focusable 属性来实现按钮的特效,看到百威啤酒的客户端主界面的按钮,感觉比较新奇,先看下图片:

注意图中我画的箭头,当时鼠标点击的黑色圈圈的位置,然后按钮出现了按下的效果(黄色的描边)

刚开始看到这种效果很是好奇,不知道是怎么实现的,后来仔细一想,应该是整个啤酒罐是一张图片(ImageView),该图片是布局在三个按钮之上,然后就是最关键的地方,把图片设置为不可获取焦点,也就是android:focusable="false" ,就这样简单的一行,就可以搞定了!

为了验证我的想法,我建了一个工程来做测试,效果如下图所示:

具体代码如下:

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.             <!-- 实心,即填充 -->   
  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.             <!-- 实心,即填充 -->   
  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> 

关于button_selector.xml中shape的使用有疑问的可以看我上次的文章:Android中shape的使用。

【编辑推荐】

Android ListView详解

Android开发中插入新的Activity

在Android应用程序中使用Internet数据

责任编辑:zhaolei 来源: 博客园
相关推荐

2010-09-10 15:16:51

CSSdisplay

2010-09-08 15:16:46

clearCSS

2010-09-09 16:54:05

CSSclear

2010-08-30 16:02:06

CSSclear

2023-02-01 08:31:48

2016-03-03 15:11:42

Spark Strea工作流调度器

2010-09-03 10:18:06

CSSdisplay:inl

2022-12-26 09:27:48

Java底层monitor

2011-06-01 14:20:37

Android

2010-07-15 14:23:42

SQL Server数

2011-08-12 11:31:46

iPhoneUIView动画

2021-06-09 11:41:10

RateLimiterJava代码

2021-01-08 08:34:09

Synchronize线程开发技术

2023-07-11 08:00:00

2011-09-07 14:55:28

Android WidAppWidget事件

2014-07-14 17:35:29

Activity

2013-04-16 14:42:38

云计算智能手机移动通信网络

2009-06-03 09:01:41

微软Windows 7操作系统

2021-10-20 07:36:03

Python构造方法

2021-01-29 08:33:39

JDK底层UUID
点赞
收藏

51CTO技术栈公众号