Android中focusable属性的妙用之底层按钮的实现
2015-01-27 21:31
155 查看
http://mobile.51cto.com/android-265446.htm
最近比较热衷于Android开发这方面,关注到了许多像下面这样对例子,分享一下。Focusable 是 Microsoft .NET 属性访问器,它实际上是一个依赖项属性。 这一特定依赖项属性非常普遍地在派生元素类(尤其是控件)中以不同方式设置其原本的“默认”值。 这种情况通常以两种方式发生
AD:WOT2015 互联网运维与开发者大会 热销抢票
在Android中使用focusable 属性来实现按钮的特效,看到百威啤酒的客户端主界面的按钮,感觉比较新奇,先看下图片:
![](http://images.51cto.com/files/uploadimg/20110530/0930550.jpg)
注意图中我画的箭头,当时鼠标点击的黑色圈圈的位置,然后按钮出现了按下的效果(黄色的描边)
刚开始看到这种效果很是好奇,不知道是怎么实现的,后来仔细一想,应该是整个啤酒罐是一张图片(ImageView),该图片是布局在三个按钮之上,然后就是最关键的地方,把图片设置为不可获取焦点,也就是android:focusable="false" ,就这样简单的一行,就可以搞定了!
为了验证我的想法,我建了一个工程来做测试,效果如下图所示:
![](http://images.51cto.com/files/uploadimg/20110530/0930551.jpg)
具体代码如下:
main.xml:
button_selector.xml:
关于button_selector.xml中shape的使用有疑问的可以看我上次的文章:Android中shape的使用。
最近比较热衷于Android开发这方面,关注到了许多像下面这样对例子,分享一下。Focusable 是 Microsoft .NET 属性访问器,它实际上是一个依赖项属性。 这一特定依赖项属性非常普遍地在派生元素类(尤其是控件)中以不同方式设置其原本的“默认”值。 这种情况通常以两种方式发生
AD:WOT2015 互联网运维与开发者大会 热销抢票
在Android中使用focusable 属性来实现按钮的特效,看到百威啤酒的客户端主界面的按钮,感觉比较新奇,先看下图片:
![](http://images.51cto.com/files/uploadimg/20110530/0930550.jpg)
注意图中我画的箭头,当时鼠标点击的黑色圈圈的位置,然后按钮出现了按下的效果(黄色的描边)
刚开始看到这种效果很是好奇,不知道是怎么实现的,后来仔细一想,应该是整个啤酒罐是一张图片(ImageView),该图片是布局在三个按钮之上,然后就是最关键的地方,把图片设置为不可获取焦点,也就是android:focusable="false" ,就这样简单的一行,就可以搞定了!
为了验证我的想法,我建了一个工程来做测试,效果如下图所示:
![](http://images.51cto.com/files/uploadimg/20110530/0930551.jpg)
具体代码如下:
main.xml:
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/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="http://schemas.android.com/apk/res/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的使用有疑问的可以看我上次的文章:Android中shape的使用。
相关文章推荐
- Android中focusable属性的妙用之底层按钮的实现
- Android中focusable属性的妙用——底层按钮的实现
- Android中focusable属性的妙用——底层按钮的实现
- Android中focusable属性的妙用——底层按钮的实现
- Android中focusable属性的妙用——底层按钮的实现
- Android中focusable属性的妙用——底层按钮的实现
- Android中focusable属性的妙用——底层按钮的实现
- Android中focusable属性的妙用——底层按钮的实现
- Android动画 实现开关按钮动画(属性动画之平移动画)实例代码
- Android用drawable实现属性按钮3种状态demo
- Android 开发过程中 Java 代码中属性 设置 底层实现
- Android中包含多个Activity的应用退出按钮实现
- Android属性 android:focusableInTouchMode
- Android中ImageButton自定义按钮的按下效果的代码实现方法
- Android的按钮单击事件及监听器的实现方式
- Android的按钮单击事件及监听器的实现方式
- Extjs 利用panel html属性加入DIV实现如TextArea的效果,并且能正确使用html标签,实现如向textarea文本域嵌入按钮button或其他控件,,而且兼容firefox
- android 自定义按钮实现 home键 和返回键
- Android中包含多个Activity的应用退出按钮实现
- Android中包含多个Activity的应用退出按钮实现