如何更改listView中的文本颜色

时间:2011-06-24 05:19:36

标签: android listview customization

实际上我想要更改我为我的应用创建的列表视图的文本颜色...当我将listview的bacground颜色更改为白色时,listview中显示的文本不会显示为文本颜色也是白色。 .so有没有办法将listview的文本颜色更改为黑色,以便它在listView中可见..我正在发送我的代码你请检查它..

XML代码:

  <LinearLayout android:id="@+id/LinearLayout01"
android:layout_width="fill_parent"
android:layout_height="fill_parent"

xmlns:android="http://schemas.android.com/apk/res/android">

<LinearLayout android:layout_height="match_parent" android:id="@+id/linearLayout1" android:layout_width="wrap_content" android:orientation="vertical">
    <LinearLayout android:layout_height="wrap_content" android:id="@+id/linearLayout2" android:layout_width="match_parent">
    <TextView android:text="All Contacts" 
        android:id="@+id/textView1" 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:background="#808080"
        android:textColor="#000000">
        </TextView>
    </LinearLayout>
 <LinearLayout android:layout_height="wrap_content" android:id="@+id/linearLayout2" android:layout_width="match_parent"> 
   <ListView android:id="@+id/ListView01"

android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</LinearLayout>
</LinearLayout>
</LinearLayout>

Java代码:

private ListView lv1;
    private String lv_arr[]={"Android","iPhone","BlackBerry","AndroidPeople","Android","iPhone"};
    @Override
    public void onCreate(Bundle icicle)
    {
        super.onCreate(icicle);
        setContentView(R.layout.contact_activity);
        lv1=(ListView)findViewById(R.id.ListView01);    
        lv1.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1 , lv_arr));

        lv1.setOnItemClickListener(new OnItemClickListener() 
        {

            public void onItemClick(AdapterView<?> parent, View view,int position, long id) 
            {

                      String selecteditem = lv_arr[position];
                      Intent myIntent = new Intent(view.getContext(), ContactInfo.class);
                      myIntent.putExtra("item", selecteditem);
                      startActivity(myIntent);
            }
        });
    }       

5 个答案:

答案 0 :(得分:3)

要更改listview项目的颜色/高度/宽度或任何外观和外观,您必须使用自定义适配器定义自定义列表视图。

首先,为listview定义listitem行文件,在其中使用任意颜色的Textview,然后将此行文件传递给自定义适配器。

以下是我的示例:tinyurl.com/buq5wdx,请查看此示例并根据您的要求进行自定义。

更新

我完全同意Pragna的答案,但是如果您仍然想要使用带有1个imageview,1个imageview和1个textview或任何控件的2个文本视图的listview,则必须通过定义自定义适配器来自定义列表视图。

答案 1 :(得分:2)

listplaceholder.xml,包含列表视图和textview

<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="@android:color/white" >
   <ListView
        android:id="@id/android:list"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:drawSelectorOnTop="false"
        android:dividerHeight="0.0px"
        android:background="@android:color/white" />
    <TextView
        android:id="@id/android:empty"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Loading"
        android:focusable="false"
        android:clickable="true"
        android:background="@android:color/white" />
</LinearLayout>

并且包含textview的list_item.xml是

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="@android:color/white">
    <TextView android:id="@+id/item_title"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" 
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:padding="2dp" 
        android:textSize="20dp"
        android:textColor="@android:color/black"
        android:background="@android:color/white" />
    <TextView android:id="@+id/item_subtitle"
        android:layout_width="match_parent" 
        android:layout_height="wrap_content"
        android:padding="2dp"
        android:textSize="12dp"
        android:textColor="@color/grey"
        android:background="@android:color/white" />
</LinearLayout>

在包含Textview put

的xml中
android:textColor="@android:color/black"
android:background="@android:color/white"

希望这会起作用

或者你可以创建一个自定义arrayadapter并覆盖其中的 getView 方法。然后,您可以从此处更改背景颜色,字体颜色以及文本的fontFace。

Typeface localTypeface1 = Typeface.createFromAsset(getAssets(), "fonts/arial.ttf");

TextView tt = (TextView) v.findViewById(R.id.item_title);
tt.setText(results.get(position).get("title"));
tt.setBackgroundColor(color);
tt.setTextColor(color);
tt.setTypeface(localTypeface1);

答案 2 :(得分:0)

重复:put color in Listview dymically

如果您想进行自定义,则无法使用 android.R.layout.simple_list_item_1

您必须创建一个新的布局,例如 my_list_row.xml

<xml version="1.0" encoding="utf-8">
<TextView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:textColor="yourcolor" />

并设置在活动

 lv1.setAdapter(new ArrayAdapter<String>(this,R.layout.my_list_item , lv_arr));

答案 3 :(得分:0)

解决方法是使用Base Adapter或Array适配器自定义列表视图。

lv1.setAdapter(Adapter Class Object). 

在Adapter类中包含 getView()方法。此方法将构建列表视图列表的视图。因此,根据需要将膨胀的xml布局添加到 getView()

答案 4 :(得分:0)

检查这些以获取更多信息:

http://www.anddev.org/view-layout-resource-problems-f27/changing-listview-text-color-t14527.html

更改android中列表视图的文本颜色

更改ListView中的文本颜色

为List项目制作布局并将其绑定到ListAdapter。有关自定义列表视图的示例,请参阅此站点:http://p-xr.com/android-tutorial-how-to-parseread-xml-data-into-android-listview/。 (帖子底部有一个Eclipse项目)

相关问题