android style select,Android美工坊:Selector选择器的使用

本文介绍了如何在Android中使用selector实现按钮和TextView的点击效果切换。通过创建XML资源文件定义不同状态下的背景,例如button_pressed和button_normal,从而在用户交互时改变视图样式。示例包括了Button的点击效果以及用TextView模拟TabWidget风格的选项卡。通过设置点击事件和焦点状态,可以实现视觉反馈和交互体验的提升。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

【IT168技术】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"?>

android:state_pressed="true"android:drawable="@drawable/button_pressed">

android:drawable="@drawable/button_normal">

res/layout/main.xml<?xml version="1.0"encoding="utf-8"?>

android:id="@+id/test"android:layout_width="fill_parent"android:layout_height="wrap_content"android:background="@drawable/myselectr"android:text="Go Home"/>

运行效果:

e21006d5e2976d9217cde39e66557648.png

这是正常情况

898559f9ea63fb2545232e0290671e91.png

这是点击后的效果

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

例子2:TextView点击效果

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

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

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

a3c2e5292f4cc0c7c22be83556af1ce4.png

点击后

5851cb5e489d34963383abdf3ddb1f79.png

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

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

<?xml version="1.0"encoding="utf-8"?>

android:startColor="#FFFFFFFF"android:endColor="#FFFFFFFF"android:angle="270.0"android:centerY="0.3"android:centerColor="#FFBDBDBD"/>

/res/drawable/button_color.xml<?xml version="1.0"encoding="utf-8"?>

android:startColor="#FF7F7F7F"android:endColor="#FF000000"android:angle="270.0"/>

<?xml version="1.0"encoding="utf-8"?>

android:startColor="#FFE5CF33"android:endColor="#FFF1E7A2"android:angle="90.0"/>

android:startColor="#FF1B1B1B"android:endColor="#FF969696"android:angle="90.0"/>

android:startColor="#FF000000"android:endColor="#FF474747"android:angle="90.0"/>

android:startColor="#FF000000"android:endColor="#FF474747"android:angle="90.0"/>

android:startColor="#FF000000"android:endColor="#FF474747"android:angle="90.0"/>

res/drawable/button_selector.xml<?xml version="1.0"encoding="utf-8"?>

android:startColor="#FFE5CF33"android:endColor="#FFF1E7A2"android:angle="90.0"/>

android:startColor="#FF1B1B1B"android:endColor="#FF969696"android:angle="90.0"/>

android:startColor="#FF000000"android:endColor="#FF474747"android:angle="90.0"/>

android:startColor="#FF000000"android:endColor="#FF474747"android:angle="90.0"/>

android:startColor="#FF000000"android:endColor="#FF474747"android:angle="90.0"/>

res/layout/main.xml,这个是主布局,由自定义的Button和1px的白色矩形组成<?xml version="1.0"encoding="utf-8"?>

android:layout_width="fill_parent"android:layout_height="10dip"/>

android:layout_width="fill_parent"android:layout_height="40dip">

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"/>

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"/>

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"/>

继承自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;publicclass TextButton extends TextView {publicTextButton(Context context)

{

super(context);

}publicTextButton(Context context, AttributeSet attrs,intdefStyle)

{

super(context,attrs,defStyle);

}publicTextButton(final Context context, AttributeSet attrs)

{

this(context,attrs,0);

this.setOnTouchListener(newOnTouchListener()

{

@OverridepublicbooleanonTouch(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();

}

returnfalse;

}

});

}

}

主程序

package com.loulijun.demo02;

import android.app.Activity;

import android.os.Bundle;publicclass Demo02Activity extends Activity {/**Called when the activityisfirst created.*/@Overridepublicvoid onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值