如何更改ListView中的文字颜色 [英] How to change the textcolor in listView

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

问题描述

其实我想改变,我已经为我的app..actually创建时,我的ListView的bacground颜色改为白色在ListView文本present不显示为文本的颜色列表视图的文本颜色也white..so有没有办法来改变列表视图黑色的文本颜色,使其在listView..i可见我送我的$ C $立方米请检查一下。

XML code:

 <的LinearLayout机器人:ID =@ + ID / LinearLayout01
机器人:layout_width =FILL_PARENT
机器人:layout_height =FILL_PARENT的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android><的LinearLayout机器人:layout_height =match_parent机器人:ID =@ + ID / linearLayout1的android:layout_width =WRAP_CONTENT机器人:方向=垂直>
    <的LinearLayout机器人:layout_height =WRAP_CONTENT机器人:ID =@ + ID / linearLayout2的android:layout_width =match_parent>
    <的TextView的android:文本=所有联系人
        机器人:ID =@ + ID / textView1
        机器人:layout_width =FILL_PARENT
        机器人:layout_height =WRAP_CONTENT
        机器人:背景=#808080
        机器人:文字颜色=#000000>
        < / TextView的>
    < / LinearLayout中>
 <的LinearLayout机器人:layout_height =WRAP_CONTENT机器人:ID =@ + ID / linearLayout2的android:layout_width =match_parent>
   < ListView的机器人:ID =@ + ID / ListView01机器人:layout_width =FILL_PARENT
机器人:layout_height =FILL_PARENT/>
< / LinearLayout中>
< / LinearLayout中>
< / LinearLayout中>

的Java code:

 私人的ListView LV1;
    lv_arr [] = {机器人,iPhone,黑莓,AndroidPeople,机器人,iPhone}私人字符串;
    @覆盖
    公共无效的onCreate(捆绑冰柱)
    {
        super.onCreate(冰柱);
        的setContentView(R.layout.contact_activity);
        LV1 =(ListView控件)findViewById(R.id.ListView01);
        lv1.setAdapter(新ArrayAdapter<串GT;(这一点,android.R.layout.simple_list_item_1,lv_arr));        lv1.setOnItemClickListener(新OnItemClickListener()
        {            公共无效onItemClick(适配器视图<>母公司,观景,INT位置,长的ID)
            {                      字符串将selectedItem = lv_arr [位置]
                      意图myIntent =新意图(view.getContext(),ContactInfo.class);
                      myIntent.putExtra(项目的,则selectedItem);
                      startActivity(myIntent);
            }
        });
    }


解决方案

有关更改颜色/高/宽或ListView项中的任何外观和外观,你必须使用自定义适配器定义自定义列表视图。

首先,定义列表项排文件为您的列表视图,其中采取一个TextView与任何你想要的,然后该行文件传递给您的自定义适配器的颜色。

下面是我的例子: tinyurl.com/buq5wdx ,通过这个例子,使定制按您的要求

更新:

我跟Pragna的回答完全同意,但如果你仍然想拥有的ListView的东西,如2 textviews 1 ImageView的,1 ImageView的和1的TextView或任何控制,你必须通过定义自定义适配器以自定义列表视图。

Actually i want to change the text color of the listview that i had created for my app..actually when i change the bacground color of listview to white the text present in the listview is not appear as text color is also white..so is there any way to change the text color of the listview to black so that it is visible in the listView..i am sending my code u please check it..

XML code:

  <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 code:

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);
            }
        });
    }       

解决方案

For changing the color/height/width or any look and appearance of listview item, you have to define your custom listview with custom adapter.

First, define listitem row file for your listview, in which take a Textview with the color whatever you want and then pass this row file to your custom adapter.

Here is my example: tinyurl.com/buq5wdx , go through this example and make customization as per your requirement.

Update:

I am totally agree with Pragna's answer, but if you still want to have listview with something like 2 textviews with 1 imageview, 1 imageview and 1 textview or any control, you have to customize the listview by defining Custom Adapter.

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

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