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

本文介绍如何在Android中利用按钮的focusable属性实现独特的按钮按下特效,通过一个实例展示了如何设置按钮背景选择器及不可获取焦点的图片,从而实现按钮按下时出现黄色描边的效果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

http://mobile.51cto.com/android-265446.htm

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

AD:WOT2015 互联网运维与开发者大会 热销抢票


    在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的使用。

    评论
    添加红包

    请填写红包祝福语或标题

    红包个数最小为10个

    红包金额最低5元

    当前余额3.43前往充值 >
    需支付:10.00
    成就一亿技术人!
    领取后你会自动成为博主和红包主的粉丝 规则
    hope_wisdom
    发出的红包
    实付
    使用余额支付
    点击重新获取
    扫码支付
    钱包余额 0

    抵扣说明:

    1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
    2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

    余额充值