本帖最后由 kerrisr 于 2010-7-6 23:03 编辑 最近使用listview遇到个问题,我把listview的background设置为#ffffff白色,然后里面的字体默认页是白色的就导致看不见了。所以我想改为其他颜色 具体代码如下: java文件: package com.kerrisr.testlistview; import java.util.ArrayList; import android.R.anim; import android.app.Activity; import android.os.Bundle; import android.widget.ArrayAdapter; import android.widget.ListView; public class MainLVActivity extends Activity { /** Called when the activity is first created. */ private ArrayList<String> lvList; private ListView lv; private ArrayAdapter<String> lvAdapter; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); lv = (ListView) findViewById(R.id.ListView01); lvList = new ArrayList<String>(); lvList.add("Iphone"); lvList.add("Ophone"); lvList.add("Lphone"); lvAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,lvList); lv.setAdapter(lvAdapter); } } 复制代码 xml文件如下: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="#ffffff"> <ListView android:id="@+id/ListView01" android:layout_width="wrap_content" android:layout_height="wrap_content"> </ListView> </LinearLayout> 复制代码 现在情况是,我不想自定义adapter和layout就使用系统默认的android.R.layout.simple_list_item_1。有没什么方法能够改变字体的颜色 ,例如改为黑色。 打包的代码: TestListView.rar (38.06 KB, 下载次数: 561) 不知道大家有什么好方法没? 分享到: QQ空间 腾讯朋友 腾讯微博 分享0 收藏0 顶0 踩0 转发到微博 举报 从三品 首席架构师 左都尉 UID 28742 博客 0 串个门 加好友 打招呼 发消息 2# kerrisr 发表于 2010-8-31 01:38:39 | 只看该作者 怎么没人回答。。V哥和thelunsu 哥进来看看啊。 举报 管理员 科学家 右将军 UID 1 博客 133 串个门 加好友 打招呼 发消息 3# Vincent 发表于 2010-8-31 01:38:56 | 只看该作者 自己定义一个listview item资源xml,使用android:textColor指定文字的颜色,比如我定义了item1.xml,内容为: <?xml version="1.0" encoding="utf-8"?> <TextView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/text1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceLarge" android:textColor="#000000" android:gravity="center_vertical" android:paddingLeft="6dip" android:minHeight="?android:attr/listPreferredItemHeight" /> 然后再改一下程序: lvAdapter = new ArrayAdapter<String>(this, R.layout.item1,lvList); 举报 管理员 科学家 右将军 UID 1 博客 133 串个门 加好友 打招呼 发消息 4# Vincent 发表于 2010-8-31 01:39:00 | 只看该作者 我定义的这个xml是照frameworks\base\core\res\res\layout\simple_list_item_1.xml 抄的,加了一行指定字体颜色。 android.R.layout.simple_list_item_1的定义就是frameworks\base\core\res\res\layout\simple_list_item_1.xml 举报 管理员 科学家 右将军 UID 1 博客 133 串个门 加好友 打招呼 发消息 5# Vincent 发表于 2010-8-31 01:39:35 | 只看该作者 如果想动态改变,就是自己实现adapter,在getView中调用: public View getView(int position, View convertView, ViewGroup parent) { if (convertView == null) { convertView = mLayoutInflater.inflate(R.layout.item1, parent, false); } mView = (TextView) convertView.findViewById(R.id.text1); mView.setText(...); mView.setTextColor(...); return convertView; } 举报 从三品 首席架构师 左都尉 UID 28742 博客 0 串个门 加好友 打招呼 发消息 6# kerrisr 发表于 2010-8-31 01:39:45 | 只看该作者 回复 5# Vincent V哥太厉害了,谢谢啊,我之前也在找那个xml,结果我找到android.jar下res里的layout发现双击后打不开好像是已经编译好的2进制的文件,你是把所有源码下载下来才找到的layout是吧? 举报 管理员 科学家 右将军 UID 1 博客 133 串个门 加好友 打招呼 发消息 7# Vincent 发表于 2010-8-31 01:40:01 | 只看该作者 我老早就把源代码都下载下来了,有备无患{:3_57:} 举报 正八品 实习工程师 水母 UID 60047 博客 0 串个门 加好友 打招呼 发消息 8# lds2012 发表于 2010-9-1 10:24:23 | 只看该作者 <ListView android:id="@id/android:list" android:cacheColorHint="#00000000" /> 在listView上面设置一个cachecolorHint后,就可以自由在list里面的元素上面设置textColor了。 举报 正八品 实习工程师 新兵 UID 112789 博客 0 串个门 加好友 打招呼 发消息 9# choclover 发表于 2010-9-4 05:13:12 | 只看该作者 Vincent 发表于 2010-7-6 23:00 自己定义一个listview item资源xml,使用android:textColor指定文字的颜色,比如我定义了item1.xml,内容为 ... 你这个写法在2.2版本已经不好用了,要改为如下的代码。 <TextView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@android:id/text1" android:layout_width="match_parent" android:layout_height="?android:attr/listPreferredItemHeight" android:paddingLeft="?android:attr/expandableListPreferredItemPaddingLeft" android:textAppearance="?android:attr/textAppearanceLarge" android:textColor="@color/black" android:gravity="center_vertical" />