简体   繁体   中英

How to set text color for ListAdapter?

I defined a ListAdapter:

setListAdapter(new ArrayAdapter<String>(TimeMode_Choose.this, android.R.layout.simple_list_item_1, Chooses));

Then I defined a background color:

getListView().setBackgroundColor(Color.BLACK);

But now my problem is that the text color of the whole list is black and I can't see the list.

How can I change the textcolor?

You can do below things to do the desired task

  1. create a custom listview or
  2. define the style as below
<?xml version="1.0" encoding="utf-8"?>
<resources>

<style name="ListFont" parent="@android:style/Widget.ListView">
    <item name="android:textColor">#FF0000</item>
    <item name="android:typeface">sans</item>
</style>

</resources>

Add this style to your Activity definition in the Manifest XML document as an android:theme attribute, and assign as value the name of the style you created.

You need to override getView() method of ArrayAdapter , and change the color of text there.

Create layout(row_layout) like this->

<?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:id="@+id/listItem"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@+id/listItem"
        android:textSize="20px" >
    </TextView>

</LinearLayout>

Now in JAVA Code ->

setListAdapter(new ArrayAdapter<String>(TimeMode_Choose.this, R.layout.row_layout, R.id.listItem, Chooses));

Change the background color the layout holding you list to a different color either in xml file or via java code.

<RelativeLayout 
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent" 
  android:layout_height="fill_parent" 
  android:background="#FFFFFF">

<ListView..../>

or

RelativeLayout rl=(RelativeLayout)findViewById(R.id.your_layout_id);
rl.setBackgroundColor(Color.WHITE);

How can I change the textcolor?

You don't need to create a custom Adapter(like Vishal suggested), you can also do this using the standard ArrayAdapter (assuming you only need one TextView ).

You just have to use this constructor, instead of your current one:

public ArrayAdapter (Context context, int resource, int textViewResourceId, T[] objects)

Now create a Layout with only a TextView and set it to whatever color you want and set your adapter like this:

setListAdapter(new ArrayAdapter<String>(TimeMode_Choose.this, R.layout.my_layout, R.Id.myTextViewsId, Chooses));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM