一、ListView的item背景(自定义颜色)

通常情况下,ListView的item背景用的是图片资源,下面跟大家分享一下使用颜色资源,即自定义一种颜色,当item聚焦、按压、选择的时候,可以显示我们自定义的颜色,好了废话不多说,直接分享代码:

1.首先在drawable目录下面新建一个list_item_color.xml,自定义我们需要的颜色

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" > <solid android:color="#87CEFF" /> <!-- 自定义为蓝色 --> </shape>

2.继续在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/list_item_selcet_color" android:state_pressed="true"/> <!-- 按压时变成我们自定义的颜色 -->
<item android:drawable="@drawable/list_item_selcet_color" android:state_selected="true"/> <!-- 选择时变成我们自定义的颜色 -->
</selector>

3.在layout目录下item布局文件中使用我们的背景,我这里是home_listview_item.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/list_item_select_bg" 将我们自定义的设置为背景即可,
android:orientation="vertical"
android:padding="5dp" >
<!-- 此处省略n行-->
</LinearLayout>

好了,大功告成,回去上传效果图,我知道童鞋们都喜欢看图,有图有真相嘛。

二、按压item时改变字体颜色

我们应该都见过类似的效果,就是当我们按下ListView的item时,在发现item背景颜色改变的同时,字体也会变色,其实实现起来也是很简单的。

1.在color目录下新建一个list_view_item_black_color.xml文件,

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true" android:color="#000000"/> <!-- 聚焦时为黑色 -->
<item android:state_pressed="true" android:color="#ffffff"/> <!-- 按下时为白色 -->
<item android:state_selected="true" android:color="#000000"/> <!-- 选择时为黑色 -->
<item android:color="#000000"/> <!-- 普通状态下为黑色 -->
</selector>

2.在layout资源目录下使用:

<!-- 此处上下省略n行 -->
<TextView
android:id="@+id/home_item_content"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="2dp"
android:text="今天心情很好"
android:textColor="@color/list_view_item_black_color"
android:textSize="18sp" />

好了,很简单,当做自己留下的笔记,备忘。

Android之背景颜色小知识(笔记)的更多相关文章

  1. Android TextView背景颜色与背景图片设置

    Android TextView 背景颜色与背景图片设置,android textview 控件,android textview 背景, android textview 图片,android te ...

  2. 改变Android按钮背景颜色的高效方法

    本文将介绍一种有效改变Android按钮颜色的方法. 按钮可以在状态改变时改变其颜色(例如按下,禁用,高亮显示).但是,这需要一一说明每个状态.这篇文章将提供你一个根据状态变化轻松改变按钮颜色的方法. ...

  3. Android的47个小知识

    1.判断sd卡是否存在  boolean sdCardExist = Environment.getExternalStorageState().equals(android.os.Environme ...

  4. Android drawable xml 各种小知识

    摘抄自网络. 圆角或者各种变种背景, <?xml version="1.0" encoding="utf-8"?> <shape xmlns: ...

  5. Android中的一些小知识

    android中开发常用快捷键alt+/ 自动补全ctrl+1 有问题提示生成接收的参数 ctrl+L 在按2 注释// 注释 ctrl+//**/ 多行注释 ctrl+shift+/ android ...

  6. Java基础小知识笔记

    1. Integer转进制的一个类2. toBinaryString,toOctalString,toHexString.(转为二进制,八进制,十六进制的方法)3. 如果·数据的大小没有超过byte/ ...

  7. SQL 小知识笔记

    1.自动生成序列号 select row_number() over(order by field1) as row_number,* from t_table

  8. PDF如何去除背景,PDF去除背景颜色

    PDF文件在使用的时候大多都是单调的白色背景,但是也有小伙伴再制作PDF文件的时候会给PDF文件添加背景颜色,会有影响文字阅读的情况,这个时候就需要把背景颜色去除了,那么该怎么做呢,不会的小伙们就跟小 ...

  9. Android简易实战教程--第三十四话《 自定义SeekBar以及里面的一些小知识》

    转载本专栏文章,请注明出处尊重原创:博客地址http://blog.csdn.net/qq_32059827/article/details/52849676:小杨的博客 许多应用可能需要加入进度,例 ...

随机推荐

  1. unity5, 在mac下多开

    mac上app的多开与app本身无关,而是系统本身的功能,使用命令 open -n 就可以实现打开某应用程序的一个新实例(自行输入man open查看含义). 参考:http://mac-how-to ...

  2. 详解php的安装模式---CGI,FASTCGI,php-fpm,mod_php,mod_cgi,mod_fcgid

    1. CGI CGI是通用网关接口,HTTP服务器使用这样的接口程序来和“其他程序”(比如PHP的解释器程序)通讯,这个“其他程序”可以使用任何计算机语言来编写,它通过CGI这个接口从HTTP服务器取 ...

  3. spawn-fcgi原理及源代码分析

    spawn-fcgi是一个小程序,作用是管理fast-cgi进程,功能和php-fpm类似,简单小巧,原先是属于lighttpd的一部分.后来因为使用比較广泛.所以就迁移出来作为独立项目了.本文介绍的 ...

  4. atitit.故障排除--- 当前命令发生了严重错误。应放弃任何可能产生的结果sql server 2008

    atitit.故障排除--- 当前命令发生了严重错误.应放弃任何可能产生的结果sql server 2008 1. 现象 1 2. 原因:::sql server的bug 或者限制,查询的时候儿使用资 ...

  5. Atitit. 软件GUI按钮与仪表盘--web服务器区--获取apache配置文件路径 linux and apache的启动、停止、重启

    Atitit.   软件GUI按钮与仪表盘--web服务器区--获取apache配置文件路径 linux and apache的启动.停止.重启 可以通过"netstat -anp" ...

  6. svn add xxx.txt 提示A (bin) xxx.txt

    [root@NGINX-APACHE-SVN iptables]# svn ci -m "add iptables.txt" Adding (bin) iptables/iptab ...

  7. maven中配置jdk版本

    1 maven 中配置 <build> <plugins> <plugin> <groupId>org.apache.maven.plugins< ...

  8. C++之把流对象当做函数参数传递

    一.编译不通过的代码: /******************************************************************************* * File ...

  9. C++中常函数内部的this指针也是const类型的

    代码中碰到一个奇怪的现象,在同样的函数中调用this指针,结果却有一个无法通过编译 // 读取连接信息 void ThirdWizardPage::ReadConnection() { QFile f ...

  10. GetLastError 错误码大全(转载)

    转载自:GetLastError GetLastError GetLastError返回的值通过在api函数中调用SetLastError或SetLastErrorEx设置.函数   并无必要设置上一 ...