开发者

how to change the color of the list items in list view

开发者 https://www.devze.com 2023-04-11 02:15 出处:网络
I am using a ListView in an Activity class. I\'m not using any XML code for the ListView. Now I am unable to change the color of the Text item in the listView.

I am using a ListView in an Activity class. I'm not using any XML code for the ListView. Now I am unable to change the color of the Text item in the listView.

I changed the background color of the ListView but am unable to change the color of the list items. I got some links from the internet but am not able to figure it out. Can anyone help me to do that with some code?

My Activity class looks like this:

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));
        li开发者_JAVA技巧stView.setBackgroundColor(Color.rgb(225, 243, 253));
        this.setListAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_expandable_list_item_1,names));

    }

Above, tv.setTextColor is set for the heading of the list items. And it is not working as it is asking me to pass an integer value. What integer value can I pass for the Color? Can any one suggest some code for changing the color of the list items?


For having colored listitem,you need to custom it.For that,prepare an xml file:

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>

Now you have to use this in your adapter like-

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

And yes,you can use Color.RED,Color.YELLOW etc.for the default colors,our you can use "#3C3C3C" (while using in xml) or Color.parseColor("3C3C3C") (while using programatically) for any colors other than default colors.


You need to create a 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;
    }

}

The list item looks like this (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>

Use the TextView api's to decorate your text to your liking

and you will be using it like this

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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号