`
1025250620
  • 浏览: 225930 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

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

阅读更多
看到百威啤酒的客户端主界面的按钮,感觉比较新奇,先看下图片:
[img]
http://images.cnblogs.com/cnblogs_com/kofi/201103/201103222037399381.png[/img]

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

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

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



具体代码如下:

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

ok,就说这么多……
分享到:
评论

相关推荐

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

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

    Android编程实现ListView中item部分区域添加点击事件功能

    本文实例讲述了Android编程实现ListView中item部分区域添加点击事件功能。分享给大家供大家参考,具体如下: ...android:focusable=false 就是禁止点击,但试了试没有效果,后来师傅提醒我,我的这个listview使

    Android listview和button点击事件冲突问题解决方法

    按照网上说的通常是在ListView的Item布局中给Button加 android:focusable="false"在Item根布局加android:descendantFocusability="blocksDescendants" 。这样做之后item确实可以点击了,但又会出现另外一个问题 点击...

    Android开发笔记 今天学到的一些属性

    安卓主要依靠xml文件来布局,否则textView多的时候布局起来会比较混乱 3.android:singleLine属性为true表示文本在一行显示 android:ellipsize属性:设置当文本过长时该如何显示(显示省略号)start、end、middle...

    Android基于TextView实现跑马灯效果

    本文实例为大家分享了Android TextView实现跑马灯效果的具体代码,供大家参考,具体内容如下 当Layout中只有一个TextView需要实现跑马灯效果时,操作如下。 在Layout的TextView配置文件中增加  android:ellipsize...

    Android 中ListView setOnItemClickListener点击无效原因分析

    最近在做项目的过程中,在使用listview的时候遇到了设置item监听事件的时候在没有回调onItemClick 方法的问题。我的情况是在item中有一个Button按钮。所以不会回调。上百度找到了解决办法有两种,如下: 1、在...

    android 的listview 内部item的布局包含checkbox控件

    1.设置checkbox的属性值为:android:focusable=”false” ,防止焦点独占,解决checkbox无法选择的问题。 2.重写Adapter,重写getView方法; 3.重写Adapter的getView方法时,为每个checkbox添加事件响应并记录选择...

    Android 中使用EditText 点击全选再次点击取消全选功能

    大部分浏览器都是这样的逻辑,这样可以提高用户体验,...android:focusable=true android:selectAllOnFocus=true 完整布局文件 &lt;?xml version=1.0 encoding=utf-8?&gt; &lt;RelativeLayout xmlns:android=http://s

    Android实现QQ登录界面遇到问题及解决方法

    先给大家炫下效果图: 首先过程中碰到的几个问题: 1、对 EditText 进行自定义...-- android:focusable=true android:focusableInTouchMode=true 把EditText默认的行为截断了! --&gt; &lt;RelativeLayout xmlns:a

    Android实现EditText控件禁止输入内容的方法(附测试demo)

    本文实例讲述了Android实现EditText控件禁止输入内容的方法。分享给大家供大家参考,具体如下: ...再者,如果真要禁止输入文本,在布局文件中添加 android:focusable=”false”, 或者在代码中使用editText.s

    Android实现类似qq微信消息悬浮窗通知功能

    当window属性设置为FLAGE_NOT_FOCUSABLE表示不需要获取焦点,也不需要接受各种输入事件,此标记会同时启用FLAGE_NOT_TOUCH_MODEL,最终事件会直接传递给下层具有焦点的Widow FLAGE_NOT_TOUCH_MODE 在此模式下,系统会...

    Android中使用TextView实现文字跑马灯效果

    通常情况下我们想实现文字的走马灯效果需要在xml文件中这样设置 &lt;TextView android:layout_width=wrap_content android:layout_height=wrap_content android:singleLine=true android:ellipsize=marquee ...

    Android TV listview及焦点处理

    Android TV listview及...android:focusable=true 这样子级view就可以获取获取焦点。 2.view中需要获取焦点需要高亮框效果,可以在view画外框: package com.cn21.ecloud.tv.ui.widget; import android.content.Con

    ListView中添加CheckBox

    解决方案:设置checkbox的android:focusable="false" 2、选择其中的checkbox,当滚动ListView的时候,会出现一些Checkbox选择错位的现象, 原因:为记住Checkbox的选择状态 解决方案:当选择Checkbox的时候,记下...

    Android开发丶自定义Dialog并设置宽高适应子布局XML

    Dialog是我们做项目很常见的一项功能点了,一般的文字弹窗原生的dialog就可以帮我们实现,然而在实际开发过程中,因为弹窗布局较为复杂,充斥着各种控件,这时候原生的Dialog显然就不能满足我们的需求了,所以我们要...

    jQuery插件Focusable.zip

    Focusable 是轻量级的 DOM 元素执行焦点库,可以设置动画叠加到其他页面。 在线演示 标签:Focusable

    Android取消EditText自动获取焦点默认行为

    xml中也找不到相应的属性可以关闭这个默认行为 解决之道:在EditText的父级控件中找一个,设置成 代码如下: android:focusable=”true” android:focusableInTouchMode=”true” 这样,就把EditText默认的行为截断...

    仿空间PopupWindow

    PopupWindow ,多种弹出方式 ,一些重要方法使用public int getAnimationStyle()——获得弹出窗口显示与消失时的动态样式the animation style public void setContentView(View contentView)——设置弹出窗口包含的...

Global site tag (gtag.js) - Google Analytics