android selector的使用源代码
- 格式:docx
- 大小:16.90 KB
- 文档页数:4
Androidselector状态选择器的使⽤详解⼀、⽬的效果越好的⽤户体验来源更直接更明显的事件反馈。
selector可以“预存”多种响应的反馈,主要以下多种状态有:android:state_selected是选中android:state_focused是获得焦点android:state_pressed是点击android:state_enabled是设置是否响应事件,指所有事件设置不同状态的表现形式,则会在不同场景下有不同状态。
如⽂字:被选中状态,未被选中状态。
selector的普通使⽤则是为对应单个控件添加以selector为背景的资源,则能达到⽬的。
联合使⽤则是基本使⽤⼀种升级。
在我们的导航栏中,常使⽤LinearLayout或者RelativeLayout包含⼀个ImageView和⼀个TextView。
图⽚⽤于直观观感,⽂字⽤于更清晰的描述。
在⼀个整体菜单被选中时,需要图⽚及⽂字都表现对应的状态。
并为保证较⼤的事件响应范围,点击事件常赋予包含图⽚和⽂字的⽗控件。
即:为LinearLayout设置点击事件,ImageView、TextView表现对应的状态。
⼆、具体实现⽂字的selector:res添加⽬录color,res/color/bg_tv_selector.xml<?xml version="1.0" encoding="utf-8"?><selector xmlns:android="/apk/res/android"><item android:color="@color/red" android:state_pressed="true" /><item android:color="@color/black" /></selector>图⽚的selector:bg_qq_iv_selector.xml<?xml version="1.0" encoding="utf-8"?><selector xmlns:android="/apk/res/android"><item android:drawable="@mipmap/b_qq_pressed" android:state_pressed="true" /><item android:drawable="@mipmap/b_qq" /></selector>使⽤shape为Button的背景图,并设置selector:bg_bt_drawable_normal.xml:<?xml version="1.0" encoding="utf-8"?><shape xmlns:android="/apk/res/android"><corners android:radius="10dp" /><strokeandroid:width="2dp"android:color="@color/black" /></shape>bg_bt_drawable_pressed.xml:<?xml version="1.0" encoding="utf-8"?><shape xmlns:android="/apk/res/android"><corners android:radius="5dp" /><strokeandroid:width="2dp"android:color="@color/blue"android:dashGap="10dp" /><gradientandroid:centerColor="@color/red"android:endColor="@color/green" /></shape>bg_bt_selector.xml:<?xml version="1.0" encoding="utf-8"?><selector xmlns:android="/apk/res/android"><item android:drawable="@drawable/bg_bt_drawable_pressed" android:state_pressed="true" /><item android:drawable="@drawable/bg_bt_drawable_normal" />activity_main.xml中使⽤:<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="/apk/res/android" xmlns:tools="/tools"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"tools:context="com.future.selectorlldemo.MainActivity"><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="horizontal"><LinearLayoutandroid:id="@+id/qq_ll"android:layout_width="0dp"android:layout_height="wrap_content"android:layout_weight="1"android:background="@color/green"android:clickable="true"android:gravity="center"android:orientation="vertical"><ImageViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:src="@drawable/bg_qq_iv_selector" /><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="QQ"android:textColor="@color/bg_tv_selector" /></LinearLayout><LinearLayoutandroid:id="@+id/weixin_ll"android:layout_width="0dp"android:layout_height="wrap_content"android:layout_weight="1"android:background="@color/blue"android:clickable="true"android:gravity="center"android:orientation="vertical"><ImageViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:src="@drawable/bg_weixin_iv_selector" /><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="WeChat"android:textColor="@color/bg_tv_selector" /></LinearLayout></LinearLayout><LinearLayoutandroid:id="@+id/text_button_ll"android:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="horizontal"><TextViewandroid:layout_width="0dp"android:layout_height="wrap_content"android:layout_weight="1"android:text="⽂字和Button"android:textColor="@color/bg_tv_selector" />android:layout_width="0dp"android:layout_height="wrap_content"android:layout_weight="1"android:background="@drawable/bg_bt_selector"android:clickable="false"android:text="确认" /></LinearLayout></LinearLayout>MainActivity.Java中应⽤效果:public class MainActivity extends AppCompatActivity implements View.OnClickListener {/*** qq登录按钮*/private LinearLayout qqLoginLL;/*** 微信登录按钮*/private LinearLayout weixinLoginLL;/*** ⽂字和Button⼀起*/private LinearLayout textButtonLL;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(yout.activity_main);qqLoginLL = (LinearLayout) findViewById(R.id.qq_ll);weixinLoginLL = (LinearLayout) findViewById(R.id.weixin_ll);textButtonLL = (LinearLayout) findViewById(R.id.text_button_ll);qqLoginLL.setOnClickListener(this);weixinLoginLL.setOnClickListener(this);textButtonLL.setOnClickListener(this);}@Overridepublic void onClick(View v) {switch (v.getId()) {case R.id.qq_ll:Toast.makeText(MainActivity.this, "你点击了QQ登录区间", Toast.LENGTH_SHORT).show();break;case R.id.weixin_ll:Toast.makeText(MainActivity.this, "你点击了WeChat登录区间", Toast.LENGTH_SHORT).show(); break;case R.id.text_button_ll:Toast.makeText(MainActivity.this, "你点击了Text_Button区间", Toast.LENGTH_SHORT).show(); break;}}}展⽰效果:三、注意细节1.默认状态放在selector的最后<?xml version="1.0" encoding="utf-8"?><selector xmlns:android="/apk/res/android"><item android:drawable="@mipmap/b_qq" /><item android:drawable="@mipmap/b_qq_pressed" android:state_pressed="true" /></selector>不能实现对应效果2.TextView selector需要放置在 res/corlor⽬录下以上就是本⽂的全部内容,希望对⼤家的学习有所帮助,也希望⼤家多多⽀持。
Android 中的ListView选中项的背景颜色怎么设置?android中ListView获得焦点的项默认是黄色的(模拟器上)现在因为需求想要自己定义被选中项的背景能实现吗?最佳答案完全可以实现,这用到了Android的Selector(根据组件的状态显示该状态对应的图片,并以此图片作为背景显示)。
把下面的XML文件保存成你自己命名的.xml文件(比如list_bg.xml),注意,这个文件相当于一个背景图片选择器,在系统使用时根据 ListView中的列表项的状态来使用相应的背景图片,什么情况使用什么图片我在下面都进行了说明。
还有,你可以把它看成是一个图片来使用,放于 drawable目录下,配置背景属性android:background="@drawable/list_bg"就能达到你需要的目的了。
<?xml version="1.0" encoding="utf-8" ?><selector xmlns:android="/apk/res/android"> <item android:state_window_focused="false"android:drawable="@drawable/没有焦点时图片背景" /><item android:state_focused="true"android:state_pressed="true" <!--双条件-->android:drawable="@drawable/非触摸模式下获得焦点并单击时的背景图片" /><item android:state_focused="false" android:state_pressed="true" android:drawable="@drawable/触摸模式下单击时的背景图片" /><item android:state_selected="true" android:drawable="@drawable/选中时的图片背景" /><item android:state_focused="true" android:drawable="@drawable/获得焦点时的图片背景" /><item android:drawable="@drawable/silver" /> <!--default color --> </selector>---------------------------------------------------------------------------------------------------------------在values下新建一个color.xml<?xml version="1.0" encoding="utf-8"?><resources><drawable name="darkgray">#808080FF</drawable><drawable name="white">#FFFFFFFF</drawable><drawable name="silver">#00ffffff</drawable> <!-- 透明色 --></resources>-------------------------------------------------------------------------------------------------------------------------------------------------main布局文件中的系统 ListView 控件,注意这不是 ListViewItem 控件。
android kotlin selector扩展函数-回复Android Kotlin Selector扩展函数:为UI控件设置状态选择器样式选择器(Selector)在Android中是一种常用的UI控件样式设置方式,可以根据控件的不同状态来显示不同的样式,例如按下、选中、禁用等状态。
在Kotlin中,我们可以通过自定义扩展函数来简化设置状态选择器样式的过程,提高代码的可读性和可维护性。
本文将一步一步地介绍如何使用Kotlin的扩展函数为Android控件设置状态选择器样式。
第一步:新建一个Selector.kt文件首先,我们需要创建一个名为Selector.kt的Kotlin文件,作为我们的扩展函数集合。
在该文件中,我们将定义几个扩展函数,用于为不同类型的控件设置不同状态下的样式。
第二步:实现扩展函数在Selector.kt文件中,我们可以定义多个扩展函数,分别用于设置不同类型控件的状态选择器样式。
例如,我们可以定义一个扩展函数用于为按钮控件设置样式:fun Button.setSelectorBackground(normalDrawable: Drawable?,pressedDrawable: Drawable?,disabledDrawable: Drawable?) {val selector = StateListDrawable().apply {addState(intArrayOf(android.R.attr.state_pressed), pressedDrawable)addState(intArrayOf(-android.R.attr.state_enabled), disabledDrawable)addState(intArrayOf(), normalDrawable)}this.background = selector}在setSelectorBackground函数中,我们使用StateListDrawable()创建一个状态选择器对象。
android presenterselector用法[android presenterselector用法]PresenterSelector是Android中的一个类,用于根据不同的条件选择合适的Presenter来展示数据。
在Android中,我们经常需要根据不同的数据类型来展示不同的UI风格,比如在一个列表中同时展示文字、图片、视频等内容。
PresenterSelector就是为了帮助我们实现这样的需求而存在的。
接下来,我将一步一步详细回答这个主题,让我们一起来深入了解PresenterSelector的用法。
第一步:导入PresenterSelector类首先,我们需要在我们的Android项目中导入PresenterSelector类。
在项目的build.gradle文件中添加如下依赖项:implementation 'androidx.leanback:leanback:1.0.0'然后,同步一下项目,确保PresenterSelector类已经被成功导入。
第二步:创建Presenter类接下来,我们需要创建一些继承自Presenter类的Presenter子类,用于展示不同类型的数据。
比如,我们可以创建一个TextPresenter类来展示文本数据,再创建一个ImagePresenter类来展示图片数据。
这些Presenter类需要实现一个createViewHolder()方法,用于创建对应类型的ViewHolder。
例如,TextPresenter类的代码如下:javapublic class TextPresenter extends Presenter {Overridepublic ViewHolder onCreateViewHolder(ViewGroup parent) { LayoutInflater inflater =LayoutInflater.from(parent.getContext());View view = inflater.inflate(yout.text_layout, parent, false);return new TextViewHolder(view);}Overridepublic void onBindViewHolder(ViewHolder viewHolder, Object item) {String text = (String) item;TextViewHolder textViewHolder = (TextViewHolder) viewHolder;textViewHolder.textView.setText(text);Overridepublic void onUnbindViewHolder(ViewHolder viewHolder) { Clean up resources if needed}private static class TextViewHolder extendsPresenter.ViewHolder {TextView textView;TextViewHolder(View itemView) {super(itemView);textView = itemView.findViewById(R.id.text);}}}在TextPresenter中,我们首先在createViewHolder()方法中创建一个ViewHolder对象,并加载对应的布局文件。
android kotlin selector扩展函数-回复对于Android开发者来说,使用Selector作为View的背景是一种常见的需求。
通常情况下,我们使用XML文件来定义Selector,然后将其应用于相应的View。
但是,当我们在Kotlin中开发应用程序时,我们可以使用扩展函数来简化这个过程,让代码更加简洁和易于理解。
在这篇文章中,我们将一步一步地回答问题:“如何使用Kotlin为Android应用程序编写Selector扩展函数?”我们将从引入Selector的概念开始,并逐步演示如何编写扩展函数来完成这个任务。
首先,我们需要了解什么是Selector。
在Android中,Selector是一种用于根据不同的状态选择渲染效果的XML文件。
我们可以定义不同的状态(例如按下、选中、禁用等),然后为每个状态定义相应的背景。
当状态改变时,相应的背景将自动切换,以提供更好的用户交互体验。
现在,让我们开始撰写扩展函数。
首先,我们需要在项目的`res`目录下创建一个新的`kotlin`文件夹。
然后,我们在该文件夹中创建一个名为`ViewExtensions.kt`的新文件。
此文件将包含我们的所有Selector扩展函数。
在开始编写扩展函数之前,我们需要添加必要的依赖项。
在项目的`build.gradle`文件中,我们需要添加以下依赖项:kotlindependencies {implementation "androidx.core:core-ktx:1.7.0"}这将允许我们使用Kotlin的扩展函数。
现在,我们可以编写我们的第一个扩展函数,用于设置View的Selector 背景。
我们首先定义一个名为`setSelectorBackground()`的函数。
这个函数将接受一个Selector资源ID作为参数,然后将其应用于View的背景。
kotlinfun View.setSelectorBackground(@DrawableRes selectorResId: Int) {background = ContextCompat.getDrawable(context, selectorResId)}在上面的代码中,我们使用了`@DrawableRes`注解来标记参数,并在函数体中使用`ContextCompat.getDrawable()`函数来获取Selector资源,并将其应用于View的背景。
Android :实现⼀个计算器--(四则运算)计算器的功能介绍:Dot 的效果展⽰:侃侃 Dot:改进的地⽅:不⾜的地⽅:补充知识:android 中的事件处理步骤如何将信息显⽰到 View(EditText)上:异常的类型:Name: Dot (从某点开始,到某点结束)功能: + 、- 、* 、/ 、%(清零,退格操作)版本:1.0Dot 与其他的计算器相⽐没有太⼤的竞争⼒,甚⾄在处理⼀些极端的问题时还存在很⼤的不⾜,例如:⼤数的四则运算,对于这个问题处理的还不够到位,⽬前只能实现⼀些简单的四则运算(只能说⾃⼰的⽔平太菜,还没有掌握⼀些实质性的内容,这个是⾃⼰需要检讨的)另外:⾮常感谢 Android ⽼师教给我们的知识,使我们可以设计出第⼀款属于⾃⼰的 app.相对于最原始的设计,更新了哪些东西呢?1、改变按钮的形状,由原始的长⽅形 -- 椭圆状,使得按钮更加形象化。
2、增加背景图⽚,使⽤户的体验效果更好。
3、增加退格操作,使得⽤户在输错信息时不⽤全盘清零,可以删除刚刚写的某个字符。
1、未实现⼀些⼤数的运算。
2、当退格退完后,再按退格时会⾃动退出(不影响总体效果,但还是有那么⼀点 bug )3、⽬前两个数操作就需要 '=' ⼀下,但是可以实现多项式的运算,只是每次都需要 '='(后续会进⾏修改,逐渐完善)a 、找到事件⽬标对象(Button),通过 findViewById(),遍历⼦对象。
b 、实现view.onClickLisenter 接⼝,重写 onClick ⽅法。
c 、在事件⽬标对象上注册,btn.setOnClickListener (实现类的对象)a 、为 EditText 定义 id -- etResultb 、通过 findViewById 获取 etResult 对象c 、通过 etResult.setText() 将信息显⽰到计算器界⾯上a 、RuntimeExceptiona、RuntimeException运⾏时异常,不要求处理(但最好处理⼀下),与业务相关,⽐较主观(每个应⽤都可以采⽤⾃⼰的⽅式进⾏处理),⼀般不严重, 对效果不会产⽣太⼤的影响。
安卓开发中selector的用法安卓开发中关于listview和button都要改变android原来控件的背景,网上的资料太杂了,现在总结下android的selector的用法。
首先android的selector是在drawable/xxx.xml中配置的。
先看一下listview中的状态:把下面的XML文件保存成你自己命名的.xml文件(比如list_item_bg.xml),在系统使用时根据ListView中的列表项的状态来使用相应的背景图片。
drawable/list_item_bg.xml<?xml version="1.0" encoding="utf-8" ?><selector xmlns:android="/apk/res/android"> <!-- 默认时的背景图片--><item android:drawable="@drawable/pic1" /><!-- 没有焦点时的背景图片--><item android:state_window_focused="false"android:drawable="@drawable/pic1" /><!-- 非触摸模式下获得焦点并单击时的背景图片--><item android:state_focused="true" android:state_pressed="true"android:drawable="@drawable/pic2" /><!-- 触摸模式下单击时的背景图片--><item android:state_focused="false" android:state_pressed="true"android:drawable="@drawable/pic3" /><!--选中时的图片背景--><item android:state_selected="true" android:drawable="@drawable/pic4" /><!--获得焦点时的图片背景--><item android:state_focused="true" android:drawable="@drawable/pic5" /></selector>使用些xml文件:第一种是在listview中配置android:listSelector="@drawable/list_item_bg"或者在listview的item中添加属性android:background=“@drawable/list_item_bg"即可实现,或者在java代码中使用:Drawable drawable = getResources().getDrawable(R.drawable.list_item_bg);ListView.setSelector(drawable);同样的效果。
本文由我司收集整编,推荐下载,如有疑问,请与我司联系使用selector 修改TextView 中字体的颜色2012/08/20 0selector 想必大家都用过了,但是在修改字体的颜色的时候还是要细心。
我们在TextView 中设置字体颜色一般使用android:textColor= @color/red但是我们在使用selector 动态修改字体颜色的时候要使用android:color= @color/red我遇到这个问题的时候是在TabActivity 中,每个Tab 在选中的时候修改为蓝色。
tab_item.xml 的代码如下:?xml version= 1.0 encoding= utf-8 ? LinearLayout xmlns:android= schemas.android/apk/res/android android:id= @+id/ll_software_tabwidget_item android:layout_width= fill_parent android:layout_height= fill_parent android:gravity= center_horizontal android:orientation= vertical ImageView android:id= @+id/iv_software_tabwidget_icon android:layout_width= 30dip android:layout_height= 30dip android:layout_marginBottom= 1dip android:layout_marginTop= 5dip android:scaleType= fitXY / TextView android:id= @+id/tv_software_tabwidget_text android:layout_width= wrap_content android:layout_height= wrap_content android:layout_marginBottom= 5dip android:textColor= @drawable/software_textcolor android:textSize= 14dip / /LinearLayout 注意android:textColor= @drawable/software_textcolor ,即software_textcolor.xml 就是selector,源码如下:?xml version= 1.0 encoding= utf-8 ? selector xmlns:android= schemas.android/apk/res/android item android:state_selected= trueandroid:color=@color/software_textColor_selected /item item android:state_selected= false android:color= @color/software_textColor_unselected /item /selector 这个文件中就是要注意的地方了,必须使用android:color= @color/software_textColor_selected ,不。
Android中的Selector的⽤法详解及实例Android中的Selector的⽤法<?xml version="1.0" encoding="utf-8"?><selector xmlns:android="/apk/res/android" ><item android:state_pressed="true" android:drawable="@drawable/bg_selected"></item><item android:drawable="@drawable/bg_unselect"></item></selector>在⼯作的时候,由于系统给出的控件不够美观,因此开发时领导常常要我更改下界⾯,⽤美⼯给的图⽚取代系统图⽚。
开始时,我只是给按钮等设置⼀下背景图⽚,这样做虽然美观了,但界⾯看起来却⽐较死板,⽐如⽤户点击了按钮后,按钮没⼀点反应。
于是我就再给控件添加上onTouch监听事件,按下后改变背景颜⾊,松⼿后再恢复原来颜⾊。
但后来发现了selector这个利器,真是喜出望外,不⽤再添加onTouch监听事件了,⽤起来也⽅便灵活。
不得不说,多和其他开发⼈员交流技术经验等还是很有必要的,特别是像我这样独⾃负责⼀个app开发的。
Android的selector要在 drawable 下配置。
其中,selector可以设置的属性有:android:state_pressed 如果是true,当被点击时显⽰该图⽚,如果是false没被按下时显⽰默认。
android:state_focused 如果是true,获得焦点时显⽰;如果是false没获得焦点显⽰默认。
android:state_selected 如果是true,当被选择时显⽰该图⽚;是false未被选择时显⽰该图⽚。
5分钟让你掌握Android的selector用法为了更好的学习和工作,动动小手收藏起来吧!selector的定义selector就是状态列表(StateList),它分为两种,一种Color-Selector 和Drawable-Selector。
1、Color-Selectorcolor-selector 就是颜色状态列表,可以跟color一样使用,颜色会随着组件的状态而改变。
文件的位置存储于/res/color/文件名.xml在Java中使用是:R.color.filename在XML中使用是:@[package]color/filename基本语法<?xml version="1.0" encoding="utf-8"?><selectorxmlns:android="/apk/res/android" ><itemandroid:color="hex_color" //颜色值,#RGB,$ARGB,#RRGGBB,#AARRGGBBandroid:state_pressed=["true" |"false"]//是否触摸android:state_focused=["true" |"false"]//是否获得焦点android:state_selected=["true" |"false"]//是否被状态android:state_checkable=["true" |"false"]//是否可选android:state_checked=["true" |"false"]//是否选中android:state_enabled=["true" |"false"]//是否可用android:state_window_focused=["true" | "false"] />//是否窗口聚焦</selector>示例在/res/color/文件夹下新建color_selector.xml<?xml version="1.0" encoding="utf-8"?><selector xmlns:android="/apk/res/android"><item android:state_pressed="true"android:color="#ffff0000"/> <!-- pressed --><item android:state_focused="true"android:color="#ff0000ff"/> <!-- focused --><item android:color="#ff000000"/> <!-- default --> </selector>调用:<Buttonandroid:id="@+id/btnsave"android:layout_width="fill_parent"android:layout_height="wrap_content"android:layout_margin="5dp"android:layout_below="@+id/top_relative"android:textColor="@color/color_selector"android:text="aikaifa" />效果:按下注意字体颜色的变化2、Drawable-Selectordrawable-selector 是背景图状态列表,可以跟图片一样使用,背景会根据组件的状态变化而变化。
浅谈android的selector背景选择器
2011-10-15 12:39:30 我来说两句
收藏我要投稿关于listview和button都要改变android原来控件的背景,在网上查找了一些资料不是很全,所以现在总结一下android的selector的用法。
首先android的selector是在drawable/xxx.xml中配置的。
先看一下listview中的状态:
把下面的XML文件保存成你自己命名的.xml文件(比如list_item_bg.xml),在系统使用时根据ListView中的列表项的状态来使用相应的背景图片。
drawable/list_item_bg.xml
<?xml version="1.0" encoding="utf-8" ?>
<selector
xmlns:android="/apk/res/android"> <!-- 默认时的背景图片-->
<item android:drawable="@drawable/pic1" />
<!-- 没有焦点时的背景图片-->
<item android:state_window_focused="false"
android:drawable="@drawable/pic1" />
<!-- 非触摸模式下获得焦点并单击时的背景图片-->
<item android:state_focused="true" android:state_pressed="true"
android:drawable="@drawable/pic2" />
<!-- 触摸模式下单击时的背景图片-->
<item android:state_focused="false" android:state_pressed="true"
android:drawable="@drawable/pic3" />
<!--选中时的图片背景-->
<item android:state_selected="true"
android:drawable="@drawable/pic4" />
<!--获得焦点时的图片背景-->
<item android:state_focused="true"
android:drawable="@drawable/pic5" />
</selector>
使用些xml文件:第一种是在listview中配置
android:listSelector="@drawable/list_item_bg"
或者在listview的item中添加属性android:background=
“@drawable/list_item_bg"即可实现,或者在java代码中使用:Drawable drawable = getResources().getDrawable(R.drawable.list_item_bg);
ListView.setSelector(drawable);同样的效果。
但是这样会出现列表有时候为黑的情况,需要加上:
android:cacheColorHint="@android:color/transparent"
使其透明。
其次再来看看Button的一些背景效果:
android:state_selected是选中
android:state_focused是获得焦点
android:state_pressed是点击
android:state_enabled是设置是否响应事件,指所有事件
根据这些状态同样可以设置button的selector效果。
也可以设置selector 改变button中的文字状态。
以下就是配置button中的文字效果:
drawable/button_font.xml
<?xml version="1.0" encoding="utf-8"?>
<selector
xmlns:android="/apk/res/android"> <item android:state_selected="true" android:color="#FFF" />
<item android:state_focused="true" android:color="#FFF" />
<item android:state_pressed="true" android:color="#FFF" />
<item android:color="#000" />
</selector>
Button还可以实现更复杂的效果,例如渐变啊等等。
drawable/button_color.xml
<?xml version="1.0" encoding="utf-8"?>
<selector
xmlns:android="/apk/res/android"> <item android:state_pressed="true">
<!-- 定义当button 处于pressed 状态时的形态。
-->
<shape>
<gradient android:startColor="#8600ff" />
<stroke android:width="2dp" android:color="#000000" /> <corners android:radius="5dp" />
<padding android:left="10dp" android:top="10dp"
android:bottom="10dp" android:right="10dp" />
</shape>
</item>
<item android:state_focused="true">
<!-- 定义当button获得focus时的形态-->
<shape>
<gradient android:startColor="#eac100" />
<stroke android:width="2dp" android:color="#333333" color="#ffffff" />
<corners android:radius="8dp" />
<padding android:left="10dp" android:top="10dp"
android:bottom="10dp" android:right="10dp" />
</shape>
</item>
</selector>
最后,需要在包含button的xml文件里添加两项。
假如是main.xml 文件,我们需要在<Button />里加两项。
android:focusable="true"
android:backgroud="@drawable/button_color"
这样当你使用Button的时候就可以甩掉系统自带的那黄颜色的背景了,实现个性化的背景,配合应用的整体布局非常之有用啊。