关于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="http://schemas.android.com/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="http://schemas.android.com/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="http://schemas.android.com/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的时候就可以甩掉系统自带的那黄颜色的背景了,实现个性化的背景,配合应用的整体布局非常之有用啊
分享到:
相关推荐
NULL 博文链接:https://2528.iteye.com/blog/1164340
android studio 自定义button边框,角度,渐变颜色按下时改变样式, 以及基础动画,为image view添加旋转,下移,渐变等样式,使用timer handler button进行控制。
android 自定义各种风格button
安卓 android 自定义 switch button,滑动开关,绝对漂亮美观
在安卓开发中,我们经常用到Button按钮这个空间,默认系统提供的Button按钮样式比较单调,不适合项目应用 * ,通常我们需要自定义Button样式,自定义Button样式有三种: * 1、使用.9格式图片作为Button背景,或者...
如何自定义CheckBox的样式 1:首先在布局文件中添加CheckBox的控件配置,如: android:id="@+id/button1" style="@style/CheckBoxStyles"//这里就是用户可以自定CheckBox的样式 android:layout_width="match_...
NULL 博文链接:https://chennaigong.iteye.com/blog/1143049
一、shape 样式:(在drawable新建–》new–》Drawable resource file 在父级标签selector添加Item ) <?xml version=1.0 encoding=utf-8?> <selector xmlns:android=...
Button 自定义点击样式,添加音效 http://blog.csdn.net/shark0017
自定义了一下Button的样式 需要的朋友可以拿去看一下效果
Android Studio 在xml文件中设置界面布局1、xml文件中给输入框editText修改下划线颜色2、设置button按钮为圆角及修改按钮颜色3、运用约束布局时预览效果和运行效果不一致 写一写今天困扰我很久的问题:如何给...
自定义UI,实现了转盘样式按钮,多功能按钮。 此控件可以设置 按钮的图片,按钮的数量,还有背景,样式,分割线的宽度,还有对称轴。
下面通过实例代码给大家介绍Android studio button 按钮 四种绑定事件的方法,具体代码如下所示: package com.geli_2.sujie.sujiegeili2testbutton; import android.os.Bundle; import android.support.v7.app....
点击首页的Button即跳出对话框,显示WIFI信息(TextView),密码输入框(EditText),取消和连接按钮(Button) 实现 根据自己实际的需求,为AlertDialog创建一个布局,在此我需要定义一个如图所示的WIFI密码输入框,故在 res/...
接下来要介绍的就是如何实现上述图中的波纹效果,这种效果如果大家没有体验过的话,可以看看百度手机卫士或者360手机卫士,里面的按钮点击效果都是这样的,另外Android 5.0以上的版本也出现了这种效果。不多说,下面...
自定义后退按钮样式演示 Xamarin表单的简单自定义导航页面后退按钮样式演示。 介绍 一个简单的演示应用程序,演示了如何使用Xamarin Forms和Custom Renderer在每个页面上更改导航页导航栏中的“后退按钮”图标和样式...
android用户界面之按钮(Button)教程实例汇 android用户界面之ProgressBar教程实例汇总 android用户界面之WebView教程实例汇总 android用户界面之GridView教程实例汇总 android用户界面之SurfaceView教程实例汇总 ...
5.2.5 使用按钮——Button 5.2.6实例——计算器 5.2.7 使用图片按钮——ImageButton 5.2.8 使用复选框——CheckBox 5.2.9实例——请同意本协议 5.2.10 使用单选框——RadioGroup 5.2.11 实例——请选择性别 5.2.12...
3.7 简易的按钮事件——Button事件处理 3.8 手机页面的转换——setContentView的应用 3.9 调用另一个Activity——Intent对象的使用 3.10 不同Activity之间的数据传递——Bundle对象的实现 3.11 返回数据到前一个...