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

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

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

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

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

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

具体代码如下:

main.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:andro
        />
</RelativeLayout>

 

button_selector.xml:

<?xml version="1.0" encoding="utf-8"?>
<selector
    xmlns:andro />
        </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的使用有疑问的可以看我上次的文章:Android中shape的使用

ok,就说这么多……

相关文章:

  • 2022-12-23
  • 2021-09-15
  • 2021-07-04
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-12-04
猜你喜欢
  • 2022-01-21
  • 2021-05-28
  • 2022-12-23
  • 2021-12-04
  • 2021-12-19
  • 2021-10-23
相关资源
相似解决方案