很抱歉,如果我重复这个问题。
我正在从收件箱中阅读短信并在列表视图中显示它们。
我可以在执行后看到listview中的SMS / s作为listview项目。
唯一的问题是,listview项目的文本颜色变得苍白。(我得到的文字不可读/可见)。
我试图改变它,但什么也没发生。
如何将此颜色更改为黑色或任何其他颜色?
这是我的 activity_view_task.xml :
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="@+id/tv_tasks"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:text="Tasks"
android:textAppearance="?android:attr/textAppearanceLarge" />
<ListView
android:id="@+id/lv_view_task"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/tv_tasks"
android:layout_centerHorizontal="true"
android:cacheColorHint="#000000"
android:textColor="#000000">
</ListView>
</RelativeLayout>
这是我将java项目添加到listview的java文件:
package com.example.myapp;
import java.util.ArrayList;
import java.util.List;
import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.database.Cursor;
import android.view.Menu;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
public class ViewTask extends Activity {
TextView tv_view_task;
ListView lv_view_task;
static String sms = "";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view_task);
tv_view_task=(TextView) findViewById(R.id.tv_view_task);
lv_view_task=(ListView) findViewById(R.id.lv_view_task);
List<String> msgList = getSms();
final ArrayAdapter<String> adapter = new ArrayAdapter<String>(getApplicationContext(),android.R.layout.select_dialog_singlechoice, msgList);
lv_view_task.setAdapter(adapter);
}
public List<String> getSms() {
List<String> labeles = new ArrayList<String>();
Uri uri = Uri.parse("content://sms/inbox");
Cursor c = getContentResolver().query(uri, null, null, null, null);
// Read the sms data and store it in the list
if (c.moveToFirst()) {
do {
String body = c.getString(c.getColumnIndex("body"));
String address = c.getString(c.getColumnIndex("address"));
sms = "From : " + address + " : " + body;
labeles.add(sms);
} while (c.moveToNext());
}
c.close();
return labeles;
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_view_task, menu);
return true;
}
}
执行后看起来像这样:
答案 0 :(得分:2)
您需要为您设置所需文本颜色的ListView项目创建自定义布局。
通过这种方式,您甚至不需要自定义适配器。
e.g。 custom_textview.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:gravity="center"
android:layout_height="wrap_content"
>
<TextView
android:id="@+id/tv"
android:textColor="@color/white"
android:layout_width="fill_parent"
android:gravity="center"
android:layout_height="fill_parent"/>
</LinearLayout>
然后,您可以将布局与 ArrayAdapter :
一起使用ArrayAdapter<String> adapter = new ArrayAdapter<String>(MainActivity.this, R.layout.custom_textview, aa);
lv.setAdapter(adapter);
答案 1 :(得分:1)
制作自定义布局名称 row_item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:ignore="HardcodedText,UselessParent" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="40dp"
android:orientation="horizontal" >
<TextView
android:id="@+id/tv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center|left"
android:layout_marginLeft="10dp"
android:text="Medium Text"
android:textColor="#000000"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textSize="16sp" />
</LinearLayout>
</LinearLayout>
全局制作自定义适配器,如:
MyAdapter adp;
现在在onCreate()方法上初始化它:
adp = new MyAdapter (YourActivity.this, yourArrayList);
class MyAdapter extends BaseAdapter {
Context c;
String[] yourarraylist;
LayoutInflater mInflater;
public CustomAdapter(Context fullImage, String[] yourarraylist) {
// TODO Auto-generated constructor stub
this.c = fullImage;
this.yourarraylist= yourarraylist;
mInflater = LayoutInflater.from(c);
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return yourarraylist.length;
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
if (convertView == null) {
convertView = mInflater.inflate(R.layout.row_item, parent,
false);
TextView tv = (TextView) convertView.findViewById(R.id.tv);
// set text here
}
return convertView;
}
}
答案 2 :(得分:0)
将此添加到您的activity_view_task.xml:
android:textColor="#000000"