更改android中List视图中Text的颜色

时间:2013-08-13 04:53:52

标签: android listview textview android-arrayadapter

我必须在列表视图中更改多个所选项目(文本视图)的颜色。我在这里做的是,当用户从列表视图中选择项目时,颜色应该更改为蓝色,当用户取消选择项目时,颜色应该更改为默认颜色(此处为黑色)。我已经通过一些教程并实现了一个小小的演示。但我没有得到,如何处理颜色变化。以下是我的代码......

activity_main.xml中

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

    <ListView
        android:layout_height="wrap_content"
        android:id="@+id/listView2"
        android:layout_width="match_parent">
    </ListView>
</LinearLayout>

listitem.xml

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

    <TextView
        android:id="@+id/textView1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="TextView"
        android:textAppearance="?android:attr/textAppearanceLarge" />

</LinearLayout>

MainActivity.java

public class MainActivity extends Activity
{
    /** Called when the activity is first created. */
    public View row;
    ListView lview;
    ListViewAdapter lviewAdapter;

    private final static String month[] = {"January","February","March","April","May",
        "June","July","August","September","October","November","December"};



    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        lview = (ListView) findViewById(R.id.listView2);
        lviewAdapter = new ListViewAdapter(this, month);

        lview.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
        lview.setAdapter(lviewAdapter);

        lview.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                    long arg3) {
                // TODO Auto-generated method stub

            }
        });
    }




}

ListViewAdapter.java

public class ListViewAdapter extends BaseAdapter
{
    ArrayList<Boolean> saved = new ArrayList<Boolean>();
    Activity context;
    String title[];
    String description[];

    public ListViewAdapter(Activity context, String[] title) {
        super();
        this.context = context;
        this.title = title;

    }

    public int getCount() {
        // TODO Auto-generated method stub
        return title.length;
    }

    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return null;
    }

    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return 0;
    }

    private class ViewHolder {
        TextView txtViewTitle;

    }




    public View getView(int position, View convertView, ViewGroup parent)
    {
        // TODO Auto-generated method stub
        ViewHolder holder;
        LayoutInflater inflater =  context.getLayoutInflater();

        if (convertView == null)
        {
            convertView = inflater.inflate(R.layout.listitem_row, null);
            holder = new ViewHolder();
            holder.txtViewTitle = (TextView) convertView.findViewById(R.id.textView1);

            convertView.setTag(holder);


        }
        else
        {
            holder = (ViewHolder) convertView.getTag();
        }

        holder.txtViewTitle.setText(title[position]);


    return convertView;
    }

}

5 个答案:

答案 0 :(得分:1)

lview.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                    long arg3) {

                 TextView textView1 = (TextView) arg1.findViewById(R.id.textView1);
                   if(lv.isItemChecked(arg2))
                         textView1.setTextColor(your selected state color);
                   else
                       textView1.setTextColor(your unselected state color);

            }
        });

答案 1 :(得分:1)

只需在res / drawable中创建selector.xml,就像我创建了下面的那个。:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
    <item android:state_pressed="true" android:color="#fff"/>
    <item android:state_enabled="false" android:color="#cc003300"/>
    <item android:color="#cc003300"></item>

</selector>

然后将其分配给xml中的textview。

 android:textColor="@drawable/selector_btn_textcolor_green"

将您的代码更新为

<TextView
        android:id="@+id/textView1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="TextView"
        android:textColor="@drawable/selector_btn_textcolor_green"
        android:textAppearance="?android:attr/textAppearanceLarge" />

答案 2 :(得分:0)

对于listitem,您可以将drawable保留为xml资源,您可以将其保存在drawable文件夹中。因此,您可以将压缩,聚焦,正常状态明确地处理为不同的颜色。

答案 3 :(得分:0)

首先,您应该获得项目的选定位置,这样您就可以通过将所选项目传递到List onItemClickListener中的适配器来完成此操作: 在您的适配器类中添加:

public void setSelectedPosition(int pos)
{
    selectedPos = pos;
} 

在您的活动中,您调用适配器的此功能:

public void selectPosition(int position) {
    ((YourCustomAdapter)getListView().getAdapter()).setSelectedPosition(position);
}

并在List的ListItemClick侦听器中:

YourList.setOnItemClickListener(new OnItemClickListener()
    {
        @Override public void onItemClick(AdapterView<?> arg0, View arg1,int position, long arg3)
        {
             selectPosition(position);

        }
    });

这里selectedPos是一个全局变量,它接受你的Selected Item,所以你在Adapter中有你选择的Item,然后你可以在getView()函数中使用它:

if(selectedPos == position)
{
   rowView.setBackgroundResource(R.color.listselect_red);       
}
else
{
   rowView.setBackgroundResource(R.color.listselect_light_grey);
}

答案 4 :(得分:0)

创建新的布局xml文件并从外部库中的simple_list_item_1.xml复制整个代码,然后根据您的需要进行更改。

custom_text.xml

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/text1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#1ca099"
android:textSize="20dp"
android:padding="5dp"
android:gravity="center_vertical"
 />

然后在activity.java文件中:

 ListView lst=(ListView) findViewById(R.id.listView);
     ArrayAdapter<String> adapter=new ArrayAdapter<String>(getApplicationContext(),R.layout.custom_text,cultural);
        lst.setAdapter(adapter);