Android ListView 文本颜色 [英] Android ListView Text Color

查看:75
本文介绍了Android ListView 文本颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将 ListView textColor 设置为黑色,因为我使用的是白色背景.

I am trying to set the ListView textColor to black, since I am using a white background.

这是我的邮件活动

public class MailActivity extends ListActivity {

    String[] listItems = { "Compose", "Inbox", "Drafts", "Sent" };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.mails);

        setListAdapter(new ArrayAdapter(this,
                android.R.layout.simple_list_item_1, listItems));

    }
}

和我的 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="@android:id/list" 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"/>

    <TextView 
        android:id="@android:id/empty" 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" 
        android:text="Empty set" 
        android:textColor="#000000" />

</LinearLayout>

我将背景设为白色,但不确定在何处将前景设为黑色.我已经在 xml 中尝试过,但看起来没有帮助.

I'm getting the background as white, but am not sure where to set the foreground to black. I've tried in the xml and looks like it's not helping.

推荐答案

好的,这里有一些你应该清楚的事情:

Ok, here are some things that you should be clear about:

  1. 您在 xml 文件中设置的背景颜色是活动的,而不是您尝试定义的 ListItems 的.
  2. 每个列表项都有自己的布局文件,如果您对列表项使用复杂的布局,应传递或扩充该文件.

我将尝试用代码示例来解释这一点:

I'll try to explain this with a code sample:

****让我们从 ListItems 布局开始** :将其保存在您的 Android 项目的 res/layout 文件夹中,并说 **list_black_text.xml

****Lets start with ListItems layout** : save it in your res/layout folder of you Android project with say **list_black_text.xml

<?xml version="1.0" encoding="utf-8"?>
<!-- Definig a container for you List Item-->
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:gravity="center_vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <!-- Defining where should text be placed. You set you text color here-->
    <TextView
        android:id="@+id/list_content"
        android:textColor="#000000"
        android:gravity="center"
        android:text="sample"
        android:layout_margin="4dip"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
    />
</LinearLayout>

好吧,准确地说是带有 TextView 的简单布局.您必须为 TextView 分配一个 id 才能使用它.

Well, a simple layout with a TextView to be precise. You must have an id assigned to TextView in order to use it.

现在来到屏幕/活动/主要布局,正如我所说,您正在使用 android:background 属性定义屏幕的背景.我看到你也在那里定义了一个 TextView,我怀疑你试图在那里定义内容/列表项,这根本不需要.

Now coming to you screen/activity/chief layout, as I said you are defining background to your screen with android:background attribute. I see you have defined a TextView there as well and I suspect you are trying to define content/list item there, which is not at all needed.

这是您编辑后的布局:

<?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="@android:id/list" android:layout_width="fill_parent"
        android:layout_height="wrap_content"/>
        <!-- REMOVED TEXT VIEW, AND KEEPING BACKGROUND WHITE -->
</LinearLayout>

最后,最重要的是,设置您的适配器.

And lastly, most importantly, set your adapter.

setListAdapter(new ArrayAdapter<String>(
            this, R.layout.list_black_text, R.id.list_content, listItems));

注意我们传递给适配器 R.layout.list_black_text 的布局资源,以及我们声明的 TextView ID 的 R.id.list_content.我也将 ArrayAdapter 更改为 String 类型,因为它是通用的.

Notice the layout resource which we are passing to adapter R.layout.list_black_text, and R.id.list_content which is TextView ID we declared. I have also changed ArrayAdapter to String type since it's generic.

我希望这能解释一切.如果您同意,请将我的回答标记为已接受.

I hope this explains everything. Mark my answer accepted if you agree.

凌乱但很好的快速修复方法
如果您不想继续进行复杂的布局定义等,您也可以通过快速修复来做到这一点.

Messy but a good quick fix way
You can also do this with a quick fix if you do not want to go ahead with complex layout defining etc.

在实例化适配器时声明一个内部类来执行此操作,以下是代码示例:

While instantiating the adapter declare an inner class to do this, here is the code sample:

    ArrayAdapter<String> adapter=new ArrayAdapter<String>(
            this, android.R.layout.simple_list_item_1, listItems){

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View view =super.getView(position, convertView, parent);

            TextView textView=(TextView) view.findViewById(android.R.id.text1);

            /*YOUR CHOICE OF COLOR*/
            textView.setTextColor(Color.BLUE);

            return view;
        }
    };

    /*SET THE ADAPTER TO LISTVIEW*/
    setListAdapter(adapter);

这篇关于Android ListView 文本颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆