簡體   English   中英

如何在列表視圖中更改列表項的顏色

[英]how to change the color of the list items in list view

我在Activity類中使用ListView。 我沒有為Lis​​tView使用任何XML代碼。 現在,我無法更改listView中Text項的顏色。

我更改了ListView的背景顏色,但無法更改列表項的顏色。 我從互聯網上獲得了一些鏈接,但無法弄清楚。 有人可以幫我做一些代碼嗎?

我的Activity類如下所示:

ListView listView;
        // Create an array of Strings, that will be put to our ListActivity
        String[] names = new String[] { "India", "Malaysia" };
        TextView tv = new TextView(getApplicationContext());
        tv.setText("Select Country");
        tv.setTextColor(012);

        listView = getListView();
        listView.addHeaderView(tv);

        listView.setCacheColorHint(Color.rgb(36, 33, 32));
        listView.setBackgroundColor(Color.rgb(225, 243, 253));
        this.setListAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_expandable_list_item_1,names));

    }

在上面,為列表項的標題設置了tv.setTextColor 它不起作用,因為它要求我傳遞一個整數值。 我可以為Color傳遞什么整數值? 誰能建議一些代碼來更改列表項的顏色?

要使列表項具有彩色,需要對其進行自定義。為此,請准備一個xml文件:

custom_listitem.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent" android:layout_height="fill_parent"  
    android:orientation="vertical">


    <TextView android:layout_width="fill_parent" android:layout_height="wrap_content"
            android:textColor="#FFFFFF"
            android:id="@+id/list_item"
            android:background="#FF0000" <!-- for red color -->
            />    

</LinearLayout>

現在,您必須像在適配器中那樣使用它-

listView.setListAdapter(new ArrayAdapter<String>(this,R.layout.custom_item,R.id.list_item,names));

是的,您可以使用Color.REDColor.YELLOW等作為默認顏色,我們可以將"#3C3C3C" (在xml中使用)或Color.parseColor("3C3C3C") (以編程方式使用)用於任何默認顏色默認顏色以外的其他顏色。

您需要創建一個CustomListAdapter。

private class CustomListAdapter extends ArrayAdapter {

    private Context mContext;
    private int id;
    private List <String>items ;

    public CustomListAdapter(Context context, int textViewResourceId , List<String> list ) 
    {
        super(context, textViewResourceId, list);           
        mContext = context;
        id = textViewResourceId;
        items = list ;
    }

    @Override
    public View getView(int position, View v, ViewGroup parent)
    {
        View mView = v ;
        if(mView == null){
            LayoutInflater vi = (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            mView = vi.inflate(id, null);
        }

        TextView text = (TextView) mView.findViewById(R.id.textView);

        if(items.get(position) != null )
        {
            text.setTextColor(Color.WHITE);
            text.setText(items.get(position));
            text.setBackgroundColor(Color.RED); 
            int color = Color.argb( 200, 255, 64, 64 );
                text.setBackgroundColor( color );

        }

        return mView;
    }

}

列表項如下所示(custom_list.xml):

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content">
<TextView  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:id="@+id/textView"
    android:textSize="20px" android:paddingTop="10dip" android:paddingBottom="10dip"/>
</LinearLayout>

使用TextView api裝飾您喜歡的文本

你會像這樣使用它

listAdapter = new CustomListAdapter(YourActivity.this , R.layout.custom_list , mList);
mListView.setAdapter(listAdapter);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM