【问题标题】:How to change the textcolor in listView如何更改 listView 中的文本颜色
【发布时间】:2011-06-24 05:19:36
【问题描述】:

实际上,我想更改为我的应用程序创建的列表视图的文本颜色。实际上,当我将列表视图的背景颜色更改为白色时,列表视图中的文本不会出现,因为文本颜色也是白色的。 .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);
            }
        });
    }       

【问题讨论】:

    标签: android listview customization


    【解决方案1】:

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

    首先,为您的列表视图定义 listitem 行文件,其中采用您想要的颜色的 Textview,然后将此行文件传递给您的自定义适配器。

    这是我的示例:tinyurl.com/buq5wdx,通过此示例并根据您的要求进行自定义。

    更新:

    我完全同意 Pragna 的回答,但是如果您仍然希望列表视图具有 2 个文本视图和 1 个图像视图、1 个图像视图和 1 个文本视图或任何控件,则必须通过定义自定义适配器来自定义列表视图。

    【讨论】:

      【解决方案2】:

      listplaceholder.xml 具有列表视图和文本视图

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

      【讨论】:

      • 你能解释一下 R.id.item_title 在哪里吗?他没有使用自定义行布局。
      • @pankaj:我也不是在谈论自定义行布局。查看编辑
      【解决方案3】:

      解决方案是使用基本适配器或阵列适配器自定义列表视图。

      lv1.setAdapter(Adapter Class Object). 
      

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

      【讨论】:

      • 我认为不需要,看我的回答。
      【解决方案4】:

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

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

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

      在 ListView 中更改文本颜色

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

      【讨论】:

        【解决方案5】:

        重复: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" />
        

        并在您的 Activity 中设置:

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2023-04-04
          • 1970-01-01
          • 2013-09-25
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多