问题描述
我试图设置ListView的文字颜色为黑色。由于我使用的是白色背景。
i am trying to set the ListView TextColor to Black. Since i am using a White Background.
下面是我的MailActivity
Here is my MailActivity
public class MailActivity extends ListActivity {
String[] listItems = { "Compose", "Inbox", "Drafts", "Sent" };
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mails);
setListAdapter(new ArrayAdapter(this,
android.R.layout.simple_list_item_1, listItems));
}
}
和我的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" android:background="#FFFFFF">
<ListView android:id="@android:id/list" android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<TextView android:id="@android:id/empty" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="Empty set" android:textColor="#000000" />
</LinearLayout>
我得到的背景为白色。但我不知道在哪里设置前景色为黑色。试图在XML和看起来像它没有帮助
i get the background as white. but am not sure where to set foreground to black. tried in the xml and looks like its not helping
推荐答案
确定这里有一些事情,你应该是清楚的:
Ok here are some thing that you should be clear about:
- 的背景色在XML文件中要设置为活动的和不ListItems的你正在试图定义。
- 每个列表项都有自己的布局文件,它应该使用的是复杂的布局列表项传递或充气,以防万一。
我试着用code示例说明这一点:
I try to explain this with a code sample :
*的的让我们开始与ListItems的布局 * 的:它保存在你的Android项目的 RES /布局
文件夹具有发言权的 list_black_text.xml
*Lets start with ListItems layout *: save it in your res/layout
folder of you android project with say list_black_text.xml
<?xml version="1.0" encoding="utf-8"?>
<!-- Definig a container for you List Item-->
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:gravity="center_vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<!-- Defining where should text be placed. You set you text color here-->
<TextView
android:id="@+id/list_content"
android:textColor="#000000"
android:gravity="center"
android:text="sample"
android:layout_margin="4dip"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
好了的TextView
是precise一个简单的布局。你必须有一个分配给TextView的一个ID,才能使用它。
Well a simple layout with a TextView
to be precise. You must have an id assigned to TextView, in order to use it.
现在来到你的屏幕/活动/首席布局,我说你要定义背景,你的屏幕与安卓背景
属性。我看你已经定义了一个文本视图有作为,我怀疑你正试图确定there.Which不是在所有需要的内容/列表项。
Now coming to you screen/activity/chief layout, as I said you are defining background to you screen with android:background
attribute. I see you have defined a text view there as well and I suspect you are trying to define content/list item there.Which is not at all needed.
继承人你编辑的布局,
<?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" android:background="#FFFFFF">
<ListView android:id="@android:id/list" android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<!-- REMOVED TEXT VIEW, AND KEEPING BACKGROUND WHITE -->
</LinearLayout>
和最后最重要的是设置您的适配器。
And lastly most important is to set your adapter.
setListAdapter(new ArrayAdapter<String>(
this,R.layout.list_black_text,R.id.list_content, listItems));
您必须注意它们传递给适配器 R.layout.list_black_text
和 R.id.list_content
是TextView的ID,我们宣布。我也改变了ArrayAdapter为String类型,因为其通用的。
You must notice the layout resource which are passing to adapter R.layout.list_black_text
and R.id.list_content
is TextView ID we declared. I have also changed ArrayAdapter to String type since its generic.
我希望这说明了一切。马克,如果你同意我的回答接受。
I hope this explains everything. Mark my answer accepted if you agree.
混乱的方式,但一个好的速战速决
你也可以用速战速决做,如果你不想与复杂的布局定义等下去。
Messy Way But a Good quick fix
You can also do this with a quick fix if you do not want to go ahead with complex layout defining etc.
在实例适配器声明的内部类要做到这一点,这里是code样品:
While instantiating the adapter declare an inner class to do this, here is the code sample:
ArrayAdapter<String> adapter=new ArrayAdapter<String>(
this,android.R.layout.simple_list_item_1, listItems){
@Override
public View getView(int position, View convertView,
ViewGroup parent) {
View view =super.getView(position, convertView, parent);
TextView textView=(TextView) view.findViewById(android.R.id.text1);
/*YOUR CHOICE OF COLOR*/
textView.setTextColor(Color.BLUE);
return view;
}
};
/*SET THE ADAPTER TO LISTVIEW*/
setListAdapter(adapter);
这篇关于Android的ListView的文本颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!