博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Android中focusable属性的妙用——底层按钮的实现
阅读量:6886 次
发布时间:2019-06-27

本文共 1931 字,大约阅读时间需要 6 分钟。

看到百威啤酒的客户端主界面的按钮,感觉比较新奇,先看下图片:

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

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

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

具体代码如下:

main.xml:

<?xml version="1.0" encoding="utf-8"?> 

<RelativeLayout xmlns:android=" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    > 
    <LinearLayout 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:orientation="vertical" > 
        <Button 
            android:layout_width="match_parent" 
            android:layout_height="wrap_content" 
            android:layout_margin="10dp" 
            android:text="button1" 
            android:background="@drawable/button_selector" 
            />    
        <Button 
            android:layout_width="match_parent" 
            android:layout_height="wrap_content" 
            android:layout_margin="10dp" 
            android:text="button2" 
            android:background="@drawable/button_selector" 
            />  
        <Button 
            android:layout_width="match_parent" 
            android:layout_height="wrap_content" 
            android:layout_margin="10dp" 
            android:text="button3" 
            android:background="@drawable/button_selector" 
            />  
    </LinearLayout> 
    <ImageView 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:src="@drawable/bg2" 
        android:focusable="false" 
        /> 
</RelativeLayout>

 

button_selector.xml:

<?xml version="1.0" encoding="utf-8"?> 

<selector 
    xmlns:android="> 
    <item android:state_pressed="true" > 
        <shape> 
            <!-- 实心,即填充 --> 
            <solid android:color="#8470FF"/> 
            <!-- 描边 --> 
            <stroke 
                android:width="2dp" 
                android:color="#FFFF00"/> 
            <!-- 圆角 --> 
            <corners 
                android:radius="5dp" /> 
            <padding 
                android:left="10dp" 
                android:top="10dp" 
                android:right="10dp" 
                android:bottom="10dp" /> 
        </shape> 
    </item>

    <item>       

        <shape> 
            <!-- 实心,即填充 --> 
            <solid android:color="#8470FF"/> 
            <corners 
                android:radius="5dp" /> 
            <padding 
                android:left="10dp" 
                android:top="10dp" 
                android:right="10dp" 
                android:bottom="10dp" /> 
        </shape> 
    </item> 
</selector>

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

ok,就说这么多……

 

 

 

转载地址:http://pvtbl.baihongyu.com/

你可能感兴趣的文章
我的友情链接
查看>>
C++ 与 JAVA 的区别
查看>>
磁盘分区脚本
查看>>
ubuntu 12.4 zeromq-2.2 binding language java
查看>>
CentOS6.2下搭建LVS(DR)+Keepalived实现高性能高可用负载均衡服务器
查看>>
Spring boot 连接多数据源
查看>>
我的友情链接
查看>>
使用FastDFS搭建图片服务器单实例篇
查看>>
Linux系统中掩耳盗铃的sudo配置
查看>>
C# yeild使用
查看>>
C语言编写cgi程序(下)
查看>>
我的友情链接
查看>>
在两台华为BAS(V5.30版本以上)间建立MPLS L2×××
查看>>
EDM营销为什么要注重邮件信誉
查看>>
使用ntopng,在Linux上搭建基于Web的网络流量监控系统
查看>>
SCDPM常见报错解答
查看>>
OA项目笔记
查看>>
引用计数 vs. GC
查看>>
jquery实用的一些方法
查看>>
质数方阵
查看>>