简体   繁体   English

如何设置ListAdapter的文本颜色?

[英]How to set text color for ListAdapter?

I defined a ListAdapter: 我定义了一个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. 将此样式作为android:theme属性添加到清单XML文档的“活动”定义中,并将创建的样式的名称指定为值。

You need to override getView() method of ArrayAdapter , and change the color of text there. 你需要重写getView()的方法ArrayAdapter ,并改变文本的颜色出现。

Create layout(row_layout) like this-> 像这样创建布局(row_layout)->

<?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 -> 现在在JAVA代码中->

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. 在xml文件中或通过java代码将包含您列表的布局的背景色更改为其他颜色。

<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 ). 您无需创建自定义的Adapter(如Vishal建议的那样),也可以使用标准的ArrayAdapter进行此操作(假设您只需要一个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: 现在,仅使用TextView创建一个Layout并将其设置为所需的任何颜色,并按如下所示设置适配器:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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