问题
Sorry if i am repeating the question.
I am reading SMS/s from inbox and showing them in the listview.
I can see the SMS/s in the listview as listview item after execution.
Only problem is that, the text color of listview item is vary pale.(text i am getting is not readable/visible).
I tried to change it, but nothing happened.
How i can change this color to black or to any other color ?
Here is my 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>
Here is my java file from where I am adding the items to the listview :
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;
}
}
After executing it looks like this :
回答1:
You will need to create a custom layout for your ListView items where you set your desired textcolor.
In this way, you do not even need a custom-adapter.
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>
Then, you can use your layout with the ArrayAdapter:
ArrayAdapter<String> adapter = new ArrayAdapter<String>(MainActivity.this, R.layout.custom_textview, aa);
lv.setAdapter(adapter);
回答2:
Make Custom layout name 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>
Make Custom Adapter globally like:
MyAdapter adp;
now initialize it on onCreate() method:
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;
}
}
回答3:
Add this to your activity_view_task.xml:
android:textColor="#000000"
来源:https://stackoverflow.com/questions/20760442/color-of-listview-item-is-very-faint