Android美工坊--selector选择器的使用(转载)
2013-01-09 17:11
423 查看
selector选择器可以让你切换自定义的背景风格,比如button、ListView、或者布局点击时候的背景切换等,都需要用到它
背景可以是自定义到颜色,或者图片资源
首先需要在你的res目录下创建drawable文件夹,然后在里面创建一个selector文件,如myselector.xml
注:不知为什么,selector里面有关focus的东西在真机上没什么效果,反而会影响使用,比如android:state_focus="true",加上它就没有效果,去掉它就可以正常使用了
默认情况下直接用下面的布局即可实现点击后即可切换背景,其实只需要两个item标签即可,当然,item标签内部可以用shape标签自定义不同的风格
例子1:button点击效果
res/drawable/myselector.xml
View Code
主程序
背景可以是自定义到颜色,或者图片资源
首先需要在你的res目录下创建drawable文件夹,然后在里面创建一个selector文件,如myselector.xml
注:不知为什么,selector里面有关focus的东西在真机上没什么效果,反而会影响使用,比如android:state_focus="true",加上它就没有效果,去掉它就可以正常使用了
默认情况下直接用下面的布局即可实现点击后即可切换背景,其实只需要两个item标签即可,当然,item标签内部可以用shape标签自定义不同的风格
例子1:button点击效果
res/drawable/myselector.xml
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); } }
相关文章推荐
- Android美工坊:Selector选择器的使用
- Android美工坊--selector选择器的使用
- Android美工坊:Selector选择器的使用
- Android Button的四种点击事件及RadioButton 的点击事件,背景选择器<selector>的使用
- Android 仿微信的图片选择器ImageSelector的使用
- JDAddressSelector京东 Android 客户端样式的级联地址选择器的使用
- android selector 背景选择器的使用
- android背景选择器selector使用方法
- android的selector,背景选择器的使用
- android中使用selector选择器改变字体颜色
- Android的selector背景选择器使用
- Android selector状态选择器的使用详解
- android背景选择器selector使用方法
- Android 关于多图片选择器Android-Ultra-Photo-Selector的使用(附源码)
- Android 仿微信的图片选择器ImageSelector的使用
- Android 中 selector 选择器的使用
- Android中多图片选择器PhotoPicker库的使用(仿微信,秒杀MultiImageSelector)自己修改的多选
- android背景选择器selector使用方法
- android selector 背景选择器的使用, button (未点击,点击,选中保持状态)效果实现
- Android——selector背景选择器的使用详解(二)