2011-05-23 42 views
5

在我的应用程序中,我想更改列表视图文本颜色。在这种情况下,我使用XML文件进行列表视图。可能吗?如果是的话,举个例子。如何更改列表视图文本颜色

gender.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" 
android:background="@drawable/topelg" 
> 

     <ListView 
     android:id="@+id/android:list" 
     android:layout_marginTop="60dip" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:textColor="#000000" 

     /> 

</LinearLayout> 

Egender.java

package com.Elgifto; 

import android.app.ListActivity; 
import android.content.Intent; 
import android.graphics.Color; 
import android.os.Bundle; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.ArrayAdapter; 
import android.widget.Button; 
import android.widget.LinearLayout; 
import android.widget.LinearLayout.LayoutParams; 
import android.widget.ListView; 

public class Egender extends ListActivity{ 
    Button b1; 
    ListView lv; 

    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.gender); 

     // b1=(Button) findViewById(R.id.butt1); 
     b1=new Button(this); 
     b1.setText("Done"); 

     //b1.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); 
     lv=(ListView) findViewById(android.R.id.list); 

     lv.addHeaderView(b1); 

     b1.setOnClickListener(new OnClickListener(){ 
       public void onClick(View v) 
       { 
        // To send a result, simply call setResult() before your 
        // activity is finished. 

        finish(); 
       } 
     }); 

     lv.setAdapter(new ArrayAdapter&lt;String&gt;(this, 
       android.R.layout.simple_list_item_single_choice, GENDER)); 

     lv.setBackgroundColor(Color.BLACK); 
     lv.setItemsCanFocus(false); 
     lv.setChoiceMode(ListView.CHOICE_MODE_SINGLE); 
     lv.setTextFilterEnabled(true); 

    } 

    private static final String[] GENDER = new String[] { 
     "Male","Female" 
    }; 
} 

回答

19

如果你想改变所有ListView物品的颜色,而不是通过默认android.R.layout.simple_list_item_single_choiceArrayAdapter你应该通过自定义列表项XML,它具有不同的TextColor属性。

例如,创建custom_list_item.xml文件夹下的布局:

<?xml version="1.0" encoding="utf-8"?> 
<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/text1" 
android:layout_width="fill_parent" 
android:layout_height="?android:attr/listPreferredItemHeight" 
android:textAppearance="?android:attr/textAppearanceLarge" 
android:gravity="center_vertical" 
android:checkMark="?android:attr/listChoiceIndicatorSingle" 
android:paddingLeft="6dip" 
android:paddingRight="6dip" 
android:textColor="#FF0000" 
/> 

然后,它传递给适配器:

new ArrayAdapter<String>(this, R.layout.custom_list_item, stringList) 

我有名单,是所有项目都红了。

+0

超级兄弟!!!!!! – Karthik 2012-01-08 13:26:34

+0

嗨我用你的上面的代码..但选定的项目不启用,即比例按钮不启用。怎么做 – user1835052 2013-03-21 04:57:43

相关问题