Android美工坊--selector选择器的使用

本文详细介绍了如何在Android应用中使用selector选择器实现按钮、ListView等组件的自定义背景切换功能,包括创建drawable文件夹、编写myselector.xml文件及在布局文件中应用。同时展示了使用shape标签自定义不同状态下的渐变背景效果,以及通过配置selector实现点击后背景的变化。此外,还提供了一个使用TextView模拟按钮实现类TabWidget选项卡的实例。

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

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

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

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

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

例子1:button点击效果

res/drawable/myselector.xml

复制代码
<?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>
复制代码

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

View Code
复制代码
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);
    }
}
复制代码
代码转载自:https://pan.quark.cn/s/9cde95ebe57a 横道图,亦称为甘特图,是一种可视化的项目管理手段,用于呈现项目的进度安排和时间框架。 在信息技术领域,特别是在项目执行与软件开发范畴内,横道图被普遍采用来监控作业、配置资源以及保障项目能按时交付。 此类图表借助水平条图示来标示各个任务的起止时间点,使项目成员与管理者可以明确掌握项目的整体发展状况。 周期表或可指代计算机科学中的“作业调度周期表”或“资源配置周期表”。 在计算机系统中,作业调度是一项核心功能,它规定了哪个进程或线程能够在中央处理器上执行以及执行的具体时长。 周期表有助于系统管理者洞察作业的执行频率和资源使用状况,进而提升系统的运作效能和响应能力。 不仅如此,周期表也可能意指数据处理或研究中的周期性文档,如在金融分析中按期更新的市场信息文档。 在压缩文件“横道图,周期表.zip”内含的“横道图,周期表.doc”文件,很可能是对某个项目或任务管理的详尽阐述,涵盖利用横道图来制定和展示项目的时间进程,以及可能牵涉的周期性作业调度或资源配置情形。 文件或许包含以下部分:1. **项目简介**:阐述项目的目标、范畴、预期成效及参与项目的团队成员。 2. **横道图详述**:具体列出了项目中的各项任务,每个任务的启动与终止时间,以及它们之间的关联性。 横道图通常涵盖关键节点,这些节点是项目中的重要事件,象征重要阶段的实现。 3. **任务配置**:明确了每个任务的责任归属,使项目成员明晰自己的职责和截止日期。 4. **进展更新**:若文件是动态维护的,可能会记录项目的实际进展与计划进展的对比,有助于识别延误并调整计划。 5. **周期表探讨**:深入说明了周期性作业的调度,如定期的会议、报告递交、...
评论 2
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值