本文介绍了在列表视图中动态更改(列表项)TextView的颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用ListView,如下所示,其文件名为browser.xml.

I am using a ListView as shown below in filename browse.xml.

<ListView
   android:id="@+id/listView1"
   android:layout_width="250dp"
   android:layout_height="match_parent"
   android:layout_alignParentLeft="true"
   android:layout_below="@+id/relativeLayout1" >

</ListView>

然后我在onCreate()方法内部填充此listView:

And I am filling this listView inside onCreate() method as:

files1=new ArrayList<String>();
File sdcard=Environment.getExternalStorageDirectory();
files1 =  getListFiles(new File(sdcard.getAbsolutePath()+File.separatorChar));
ArrayAdapter<String> fileList =new ArrayAdapter<String>(this, R.layout.row,files1);

setListAdapter(fileList);

row.xml如下所示:

row.xml is shown below as:

<?xml version="1.0" encoding="utf-8"?>
<TextView
   xmlns:android="http://schemas.android.com/apk/res/android"
   android:id="@+id/rowtext"
   android:layout_width="fill_parent"
   android:layout_height="40dp"
   android:textSize="20dp"
   android:textColor="#000000"
   android:background="#FFFFFF"/>

整个程序在listView上显示sdcard的所有文件,并在单击任何listitem时,将该文件名保存到sharedPrefernce中.现在,我想在ListView中更改文件名(列表项)的文本颜色在SharedPrefernce中.

This whole program is am showing all the files of sdcard at listView and on click of any listitem, am saving that file name into sharedPrefernce..Now I want to change the text color of file name(List items) in the ListView which are there in SharedPrefernce..

:这是在使用ArrayAdapter默认构造函数列出列表视图中的所有项目

请给我一些建议.谢谢.

Pls suggest me something..Thanks..

推荐答案

我们可以在Adapter的getView()中动态更改列表项的文本颜色.

We can change the textcolor dynamically for list item in the getView() of Adapter.

public View getView(int position, View convertView, ViewGroup parent) {
    View row = convertView;

    if (row == null) {
        row = LayoutInflater.from(parent.getContext()).inflate(R.layout.row, null);
    }

    TextView listTitle = (TextView) row.findViewById(R.id.rowtext);
    listTitle.setTextColor(Color.parseColor("#405478"));

    return listTitle;
}

这篇关于在列表视图中动态更改(列表项)TextView的颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-17 22:57
查看更多