当前位置: 首页 > 编程日记 > 正文

Android美工坊:Selector选择器的使用

Android selector选择器可以让你切换自定义的背景风格,比如button、ListView、或者布局点击时候的背景切换等,都需要用到它

背景可以是自定义到颜色,或者图片资源

首先需要在你的res目录下创建drawable文件夹,然后在里面创建一个selector文件,如myselector.xml

注:不知为什么,selector里面有关focus的东西在真机上没什么效果,反而会影响使用,比如android:state_focus="true",加上它就没有效果,去掉它就可以正常使用了

默认情况下直接用下面的布局即可实现点击后即可切换背景,其实只需要两个item标签即可,当然,item标签内部可以用shape标签自定义不同的风格

例子1:button点击效果

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
    <item 
        android:state_pressed="true"
        android:drawable="@drawable/button_pressed"
        ></item>
    <item 
        android:drawable="@drawable/button_normal"
        ></item>
</selector>

res/layout/main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
    <Button
        android:id="@+id/test"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/myselectr"
        android:text="Go Home"
        />
</LinearLayout>

运行效果:

这是正常情况

这是点击后的效果

当然,针对button的selector还有很多其他的配置,但是对于一般程序来说上面的配置就够了

例子2:TextView点击效果

这个例子是网上找的,演示的是一个用TextView来定义的一个Button,实现类似TabWidget风格的选项卡。

自定义按钮,这里没有通过Button类或者子类去做派生,而是通过TextView派生出来的。

在这里三个按钮是三个TextView派生类实例,中间的白线,是1px宽的白色矩形,这样就可以做出类似上面的效果。先看图

点击后

转自:http://marshal.easymorse.com/archives/3059

/res/drawable/background_color.xml 用shape标签自定义一个渐变背景

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
    <gradient 
        android:startColor="#FFFFFFFF"
        android:endColor="#FFFFFFFF"
        android:angle="270.0"
        android:centerY="0.3"
        android:centerColor="#FFBDBDBD"
        />
</shape>

/res/drawable/button_color.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
    <gradient 
        android:startColor="#FF7F7F7F"
        android:endColor="#FF000000"
        android:angle="270.0"
        />
</shape>

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" 
    android:constantSize="true">
    <!-- 获得焦点时的背景图片 -->
    <item android:state_focused="true">
        <shape>
            <gradient 
                android:startColor="#FFE5CF33"
                android:endColor="#FFF1E7A2"
                android:angle="90.0"
                />
        </shape>
    </item>
    <!-- 设置相应所有事件 -->
    <item android:state_enabled="true" android:state_pressed="false">
        <shape>
            <gradient 
                android:startColor="#FF1B1B1B"
                android:endColor="#FF969696"
                android:angle="90.0"
                />
        </shape>
    </item>
    <!-- 按钮点击时的背景 -->
    <item android:state_enabled="true" android:state_pressed="true">
        <shape>
            <gradient 
                android:startColor="#FF000000"
                android:endColor="#FF474747"
                android:angle="90.0"
                />
        </shape>
    </item>
    <item android:state_enabled="false" android:state_pressed="true">
        <shape>
            <gradient 
                android:startColor="#FF000000"
                android:endColor="#FF474747"
                android:angle="90.0"
                />
        </shape>
    </item>
    <!-- 默认情况下的背景 -->
    <item>
        <shape>
            <gradient 
                android:startColor="#FF000000"
                android:endColor="#FF474747"
                android:angle="90.0"
                />
        </shape>
    </item>
</selector>

res/drawable/button_selector.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" 
    android:constantSize="true">
    <!-- 获得焦点时的背景图片 -->
    <item android:state_focused="true">
        <shape>
            <gradient 
                android:startColor="#FFE5CF33"
                android:endColor="#FFF1E7A2"
                android:angle="90.0"
                />
        </shape>
    </item>
    <!-- 设置相应所有事件 -->
    <item android:state_enabled="true" android:state_pressed="false">
        <shape>
            <gradient 
                android:startColor="#FF1B1B1B"
                android:endColor="#FF969696"
                android:angle="90.0"
                />
        </shape>
    </item>
    <!-- 按钮点击时的背景 -->
    <item android:state_enabled="true" android:state_pressed="true">
        <shape>
            <gradient 
                android:startColor="#FF000000"
                android:endColor="#FF474747"
                android:angle="90.0"
                />
        </shape>
    </item>
    <item android:state_enabled="false" android:state_pressed="true">
        <shape>
            <gradient 
                android:startColor="#FF000000"
                android:endColor="#FF474747"
                android:angle="90.0"
                />
        </shape>
    </item>
    <!-- 默认情况下的背景 -->
    <item>
        <shape>
            <gradient 
                android:startColor="#FF000000"
                android:endColor="#FF474747"
                android:angle="90.0"
                />
        </shape>
    </item>
</selector>

res/layout/main.xml,这个是主布局,由自定义的Button和1px的白色矩形组成
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/background_color"
    android:orientation="vertical" >

<LinearLayout 
        android:layout_width="fill_parent"
        android:layout_height="10dip"
        />
    <LinearLayout 
        android:layout_width="fill_parent"
        android:layout_height="40dip"
        >
        <com.loulijun.demo02.TextButton
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="1"
            android:text="饮食"
            android:gravity="center"
            android:background="@drawable/button_selector"
            android:focusable="true"
            android:clickable="true"
            />
        <View android:layout_width="2px" android:layout_height="fill_parent"
            android:background="#FFFFFFFF"/>
        <com.loulijun.demo02.TextButton
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="1"
            android:text="旅行"
            android:gravity="center"
            android:background="@drawable/button_selector"
            android:focusable="true"
            android:clickable="true"
            />
        <View android:layout_width="2px" android:layout_height="fill_parent"
            android:background="#FFFFFFFF"/>
        <com.loulijun.demo02.TextButton
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="1"
            android:text="体育"
            android:gravity="center"
            android:background="@drawable/button_selector"
            android:focusable="true"
            android:clickable="true"
            />
    </LinearLayout>
</LinearLayout>

继承自TextView的自定义Button

package com.loulijun.demo02;
import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
public class TextButton extends TextView {
    public TextButton(Context context)
    {
        super(context);
    }
    public TextButton(Context context, AttributeSet attrs, int defStyle)
    {
        super(context,attrs,defStyle);
    }
    public TextButton(final Context context, AttributeSet attrs)
    {
        this(context,attrs,0);
        this.setOnTouchListener(new OnTouchListener()
        {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if(event.getAction()==MotionEvent.ACTION_CANCEL
                        ||event.getAction()==MotionEvent.ACTION_UP
                        ||event.getAction()==MotionEvent.ACTION_OUTSIDE)
                {
                    Toast.makeText(context, "hello", Toast.LENGTH_SHORT).show();
                }
                return false;
            }
            
        });
    }
}

主程序

package com.loulijun.demo02;
import android.app.Activity;
import android.os.Bundle;
public class Demo02Activity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
}








本文转自 wws5201985 51CTO博客,原文链接:http://blog.51cto.com/wws5201985/812711,如需转载请自行联系原作者

相关文章:

C#中判断空字符串的3种方法性能分析【月儿原创】

C#中判断空字符串的3种方法性能分析 作者&#xff1a;清清月儿 主页&#xff1a;http://blog.csdn.net/21aspnet/ 时间&#xff1a;2007.4.28 3种方法分别是&#xff1a;string a"";1.if(a"")2.if(aString.Empty)3.if(a.Length0) 3种方法都是…

微软职位内部推荐-SDEII

微软近期Open的职位:Title: Software Development Engineer 2Group: Bing Client, Search Technology Center Asia, BingWork Location: Beijing/Suzhou, China Group OverviewSearch Technology Center Asia (STCA)STCA was founded in year 2005 and is now starting the sec…

WAIC剪影:AI的未来,关乎星辰大海

“天文学&#xff0c;是像数学一样的基础学科&#xff0c;而越是基础学科&#xff0c;就越难直接应用。”“我们没有想过盈利&#xff0c;这些技术目前来看也不太可能直接应用到其他领域。”“不管是优图还是腾讯公司层面&#xff0c;不是做的每件事情都要考虑它的经济价值或者…

用Swift实现一款天气预报APP(三)

这个系列的目录&#xff1a; 用Swift实现一款天气预报APP&#xff08;一&#xff09; 用Swift实现一款天气预报APP&#xff08;二&#xff09; 用Swift实现一款天气预报APP&#xff08;三&#xff09; 通过前面的学习&#xff0c;一个天气预报的APP已经基本可用了。至少可以查看…

asp.net2.0学习历程 菜鸟到中级程序员的飞跃【月儿原创】

asp.net2.0学习历程 菜鸟到中级程序员的飞跃 --30本好书点评 作者&#xff1a;清清月儿 主页&#xff1a;http://blog.csdn.net/21aspnet/ 时间&#xff1a;2007.5.16 学历历程 如果你是一个菜鸟或者自认为初学者那么本文非常适合你&#xff1b; 不能说这30本书…

了解黑客的关键工具---揭开Shellcode的神秘面纱

2019独角兽企业重金招聘Python工程师标准>>> ref: http://zhaisj.blog.51cto.com/219066/61428/ 了解黑客的关键工具---揭开Shellcode的神秘面纱 对于初期接触网络安全的人来说&#xff0c;Shellcode是很神秘的东西&#xff0c;对于网络攻击过程中的嗅探信息、漏洞…

2021年移动云API应用创新开发大赛火热开启!

每一位开发者&#xff0c;都是这个时代宝贵的财富2021年移动云API应用创新开发大赛以“创新云转型&#xff0c;智慧云服务”为主题旨在激发开发者创新动力丰富云计算应用场景与移动云携手探索数智未来给社会带来更多智慧创新体验大赛官方报名通道已开启您可通过下方二维码报名参…

Android 多媒体综述

Android 多媒体综述 多媒体系统是Android中最为庞大的系统&#xff0c;涉及了硬件抽象层、编解码、OpenCore多媒体框架、Android多媒体框架、Java层接口多方面的内容。一、引言本系列内容都是在Android应用层面的&#xff0c;将会分为Camera、Audio、Video三部分进行讲述。另外…

asp.net2.0导出pdf文件完美解决方案【月儿原创】

asp.net2.0导出pdf文件完美解决方案 作者&#xff1a;清清月儿 主页&#xff1a;http://blog.csdn.net/21aspnet/ 时间&#xff1a;2007.5.28 PDF简介&#xff1a;PDF&#xff08;Portable Document Format&#xff09;文件格式是Adobe公司开发的电子文件格式。这…

MYSQL 部分事务

MYSQL 中通过 savepoint 的方式来实现只提交事务的一部分。 step 1 : savepoint savepoint_name;、 做标记 step 2 :rollbak to savepoint savepoint_name;回滚到标记点 setp 3 :release savepoint savepoint_name;解除标记 -------------------------------------------------…

二维已经 OUT 了?3DPose 实现三维人体姿态识别真香 | 代码干货

作者|李秋键出品|AI科技大本营(ID:rgznai100)引言人体姿态估计是计算机视觉领域很多研究工作的基础&#xff0c;也是研究的热点问题&#xff0c;在行为识别、人机交互、姿态跟踪等领域有着广泛的应用前景。按照人体姿态维度的差异&#xff0c;可以将人体姿态估计任务分为二维人…

python学习------tab补全

python学习------tab补全 python也可以进行tab键补全 123456789101112131415161718#!/usr/bin/env python# -*- coding: utf-8 -*-# python startup fileimport sys import readline import rlcompleter import atexit import os # tab completionreadline.parse_and_bind(tab:…

asp.net的Ajax学习进阶

asp.net的Ajax学习进阶 作者&#xff1a;清清月儿 主页&#xff1a;http://blog.csdn.net/21aspnet/ 时间&#xff1a;2007.6.3 1.什么是Ajax? 2006年忽如一夜春风来&#xff0c;众多涉及到Web开发的站点都在谈Ajax&#xff0c;那么到底什么是Ajax呢&#xff1f…

Windows下FFmpeg高速入门

本系列文章导航 Windows下FFmpeg高速入门 ffmpeg參数解释 mencoder和ffmpeg參数具体解释&#xff08;Java处理视频&#xff09; Java 生成视频缩略图(ffmpeg) 使用ffmpeg进行视频文件转换成FLV整理 java 视频处理 mencoder java 视频处理 ffmpedmencoder Windows下FFmpeg高速入…

“香山”处理器产生背后的逻辑

作者 | 老石谈芯的老石来源 | 老石谈芯在最近召开的RISC-V中国峰会上&#xff0c;中科院计算所的包云岗研究员团队正式发布了名为“香山”的开源高性能RISC-V处理器。前不久我有幸和包老师就这个事情做了一次深度的交流&#xff0c;我们聊了关于RISC-V、还有“香山”处理器的前…

第79天:jQuery事件总结(二)

上一篇讲到jQuery中的事件&#xff0c;深入学习了加载DOM和事件绑定的相关知识&#xff0c;这篇主要深入讨论jQuery事件中的合成事件、事件冒泡和事件移除等内容。 一、合成事件 jQuery有两个合成事件——hover()方法和toggle()方法&#xff0c;同ready()方法一样&#xff0c;这…

asp.net利用RAR实现文件压缩解压缩【月儿原创】

asp.net利用RAR实现文件压缩解压缩 作者&#xff1a;清清月儿 主页&#xff1a;http://blog.csdn.net/21aspnet/ 时间&#xff1a;2007.6.13 如果服务器上安装了RAR程序&#xff0c;那么asp.net可以调用RAR实现文件压缩与解压缩。 不过要注意的是&#xff0c;由…

缺少HTML Doctype造成的样式问题

很简单的一个登陆界面: 代码&#xff1a; <html> <head><style type"text/css">form span {display: block;font-size: 1em;color: #787878;padding-bottom: 5px;font-weight: 600;font-family: Open Sans, sans-serif; }body{background-color: #…

快收藏!整理了 100 个 Python 小技巧

作者&#xff1a;小F来源&#xff1a; 法纳斯特目前Python可以说是非常流行&#xff0c;在目前的编程语言中&#xff0c;Python的抽象程度是最高的&#xff0c;是最接近自然语言的&#xff0c;很容易上手。你可以用它来完成很多任务&#xff0c;比如数据科学、机器学习、Web开发…

--single-transaction 参数对应MyISAM引擎和InnoDB引擎

结论&#xff1a;使用--single-transaction 备份含有MyISAM的表不会获得一致性备份&#xff0c;所有的innodb表可以获得事务开始时的一致性快照&#xff0c;但是MyISAM表获取的是备份该表时的最新快照&#xff0c; 测试库&#xff1a;test&#xff0c;包含表t1,t2,t3,t4,t5,t6 …

C#优化字符串操作【月儿原创】

C#优化字符串操作 作者&#xff1a;清清月儿 主页&#xff1a;http://blog.csdn.net/21aspnet/ 时间&#xff1a;2007.6.17 开发中可以说几乎随时会涉及到字符串处理&#xff0c;本人觉得很有必要把平时遇到的问题和大家一起讨论&#xff0c;如果大家有好的见解和…

构筑超异构计算时代,英特尔 AI 全布局

作者 | 伍杏玲出品 | AI 科技大本营&#xff08;ID:rgznai100&#xff09;我们正值数据井喷时代&#xff0c;据 IDC 发布《数据时代 2025》报告显示&#xff0c;全球每年产生的数据将从 2018 年的 33ZB 增长到 2025 年的 175ZB。其中大部分为非结构化数据&#xff0c;对数据实时…

[软件推荐]电子日记本EDiary,记下您 的每一天

推荐一款电子日记本EDiary&#xff0c;可以记下每天的工作与生活&#xff0c;可支持附件上传&#xff0c;使用方便简单&#xff0c;我从08年开始使用至今&#xff0c;感觉非常不错&#xff0c;也介绍给同事朋友使用&#xff0c;现给大家分享一下。可以到这里下载&#xff1a;ht…

C#的6种常用集合类大比拼【月儿原创】

C#的6种常用集合类大比拼 作者&#xff1a;清清月儿 主页&#xff1a;http://blog.csdn.net/21aspnet/ 时间&#xff1a;2007.6.27 说明&#xff1a;MSDN没有说出几种集合类其间的区别&#xff0c;当然欲知更多细节可参考MSDN。 一.先来说说数组的不足&#xf…

“35岁才是一个程序员成熟的开始!”

作者 | 王晓波&#xff0c;同程旅行机票事业群CTO【写在前面】不知道从什么时候开始&#xff0c;身边的“小朋友”们都开始为一件事感到焦虑&#xff0c;那就是&#xff1a;“到了35岁我还能找到一份编程的工作吗&#xff1f;”。坦白讲&#xff0c;我年轻的时候也有过迷茫的时…

centos安装easy_instal

easy_install与yum类似&#xff0c;使用easy_install&#xff0c;可以轻松在pypi软件库里面搜索python各类软件安装easy_install比较简单&#xff0c;如果配置好yum&#xff0c;就可以直接搜索python-setuptoolsyum –y install python-setuptools安装完python-setuptools之后&…

如何用xmanager远程连接centos6.0的桌面

在centos6.0系统上设置 修改custom.conf文件 vim /etc/gdm/custom.conf 在[security]下面添加 AllowRemoteRoottrue 在[xdmcp]下面添加 Port177 Enable1 修改完后效果如下&#xff1a; [daemon] [security] AllowRemoteRoottrue [xdmcp] Port177 Enable1 [greeter] [chooser] […

ASP.NET 3.5 企业级开发

议题 .NET Framework 3.5 和Visual Studio 2008 C# 面向对象程序设计 ASP.NET 状态管理和页面传值 ASP.NET 中的错误处理 ADO.NET与数据访问 架构与模式 安全与性能 优秀的团队开发管理功能C# 面向对象程序设计封装继承性多态性抽象类接口装箱和拆箱泛型ASP.NET 状态管理…

DEV报表之条形码

今天无意间发现DEV的报表居然自带条形码生成控件&#xff0c;正好要用到&#xff0c;省的自己手动生成图片了。前提&#xff1a;一张做好的报表Dev报表基础教程首先拖出XRBarCode控件&#xff0c;放到表头的空白位置&#xff0c;摆好高度宽度。选择Symbology&#xff0c;设置你…

Copilot 真会砸了程序员的饭碗?

作者 | 马超 责编 | 孙胜出品 | CSDN&#xff08;ID&#xff1a;CSDNnews&#xff09;最近OpenAI与GitHub联合构建的AI自动编程工具Copilot正式登场&#xff01;Copilot基于自然语言处理模型GPT-3搭建而成&#xff0c;可在程序员编写代码时提供建议&#xff0c;甚至直接补…