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

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

我们提供的服务有:成都做网站、网站制作、微信公众号开发、网站优化、网站认证、尚义ssl等。为成百上千企事业单位解决了网站和推广的问题。提供周到的售前咨询和贴心的售后服务,是有科学管理、有技术的尚义网站制作公司

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

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

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

具体代码如下:

main.xml:

 
 
 
 
  1.    
  2.     android:layout_width="fill_parent"   
  3.     android:layout_height="fill_parent"   
  4.     >   
  5.     
  6.         android:layout_width="match_parent"   
  7.         android:layout_height="wrap_content"   
  8.         android:orientation="vertical" >   
  9.         
  10.             android:layout_width="match_parent"   
  11.             android:layout_height="wrap_content"   
  12.             android:layout_margin="10dp"   
  13.             android:text="button1"   
  14.             android:background="@drawable/button_selector"   
  15.             />      
  16.         
  17.             android:layout_width="match_parent"   
  18.             android:layout_height="wrap_content"   
  19.             android:layout_margin="10dp"   
  20.             android:text="button2"   
  21.             android:background="@drawable/button_selector"   
  22.             />    
  23.         
  24.             android:layout_width="match_parent"   
  25.             android:layout_height="wrap_content"   
  26.             android:layout_margin="10dp"   
  27.             android:text="button3"   
  28.             android:background="@drawable/button_selector"   
  29.             />    
  30.        
  31.     
  32.         android:layout_width="wrap_content"   
  33.         android:layout_height="wrap_content"   
  34.         android:src="@drawable/bg2"   
  35.         android:focusable="false"   
  36.         />   
  37.  

button_selector.xml:

 
 
 
 
  1.    
  2.     xmlns:android="http://schemas.android.com/apk/res/android">   
  3.        
  4.            
  5.                
  6.                
  7.                
  8.             
  9.                 android:width="2dp"   
  10.                 android:color="#FFFF00"/>   
  11.                
  12.             
  13.                 android:radius="5dp" />   
  14.             
  15.                 android:left="10dp"   
  16.                 android:top="10dp"   
  17.                 android:right="10dp"   
  18.                 android:bottom="10dp" />   
  19.            
  20.        
  21.  
  22.              
  23.            
  24.                
  25.                
  26.             
  27.                 android:radius="5dp" />   
  28.             
  29.                 android:left="10dp"   
  30.                 android:top="10dp"   
  31.                 android:right="10dp"   
  32.                 android:bottom="10dp" />   
  33.            
  34.        
  35.  

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

【编辑推荐】

Android ListView详解

Android开发中插入新的Activity

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

网站题目:Android中focusable属性的妙用之底层按钮的实现
文章位置:http://www.shufengxianlan.com/qtweb/news18/211568.html

网站建设、网络推广公司-创新互联,是专注品牌与效果的网站制作,网络营销seo公司;服务项目有等

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联