我可以更改ListVIew文本颜色吗?

时间:2014-04-01 07:58:13

标签: android android-listview

谢谢大家!我是Android的开始。我有一点问题。我使用ListView,当我运行这个程序时,我的所有列表项都是白色!这个文字怎么颜色黑色或者anthor请任何人帮助我!!

enter image description here

package com.example.shikkokoverflow_listview;


import android.os.Bundle;
import android.app.ListActivity;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

public class MainActivity extends ListActivity {

    String[] country={"Bangladesh","usa","america","india","Florida"};
    ArrayAdapter<String> adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //setContentView(R.layout.activity_main);

        adapter=new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_list_item_1, country);
        setListAdapter(adapter);


    }

    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
        // TODO Auto-generated method stub
        //super.onListItemClick(l, v, position, id);
        Toast.makeText(getApplicationContext(), country[position], Toast.LENGTH_LONG).show();

    }


}

6 个答案:

答案 0 :(得分:2)

getListView().setCacheColorHint(Color.rgb(36, 33, 32));

答案 1 :(得分:1)

使用自定义布局。使用您自己的View定义布局文件,您可以根据需要自定义字体,字体颜色,字体大小,图像......然后在ArrayAdapter声明中使用它。

myAdapt = new MyArrayArrayAdapter(this, R.id.your_layout, list);

答案 2 :(得分:1)

res/layout中制作新的布局文件。在那里制作根元素,TextView(不能有任何孩子)。然后设置所有必要的属性以及该集合android:textColor="#000000"。在您的代码中,ArrayAdapter写下这个:

    adapter=new ArrayAdapter<String>(getApplicationContext(), R.layout.new_layout, country);
    setListAdapter(adapter);

答案 3 :(得分:1)

在这里,执行以下步骤

  • 转到sdk文件夹\ sdk \ platforms \ android- \ data \ res \ layout
  • 复制simple_list_item_1并将其粘贴到您的项目res \ layout文件夹
  • 现在打开\ res \ layout \ simple_list_item_1
  • 在那里添加颜色属性。
  • 然后改变你的 adapter=new ArrayAdapter<String>(getApplicationContext(),android.R.layout.simple_list_item_1, country);

    adapter=new ArrayAdapter<String>(getApplicationContext(), R.layout.simple_list_item_1, country);

答案 4 :(得分:0)

您将使用自定义适配器的过程而不是预定义的适配器进入自定义列表适配器...

您可以在类中获得自定义适配器......

 ListView listView = (ListView)findViewById(R.id.listview);
CustomAdapter mAdapter = new CustomAdapter(this, R.layout.listitem, mListItems);//listitem is your custom layout.....
listView .setAdapter(mAdapter);

自定义适配器类,您只需在项目中添加....

public class CustomAdapter extends ArrayAdapter<Sample> {

public ArrayList<Sample> mlist;
public Context context;
public LayoutInflater inflater; 
private LinearLayout layout;
private View view;
private View mLastView;

public CustomAdapter(Context context, int resource, ArrayList<Sample> mlist) {
    super(context, resource);
    this.mlist = mlist;     
    this.context = context;     
    inflater = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

@Override
public int getPosition(Sample item) {
    return super.getPosition(item);
}

@Override
public Sample getItem(int position) {
    return mlist.get(position);
}

@Override
public int getCount() {
    return mlist.size();
}

@Override
public long getItemId(int position) {
    return super.getItemId(position);
}

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
     view = inflater.inflate(R.layout.listitem, null);//listitem will be a your cutom layout here i use two textview in the same item...
    layout = (LinearLayout)view.findViewById(R.id.linearlayoutSample);;
    TextView text1 = (TextView) view.findViewById(R.id.item1);
    TextView text2 = (TextView) view.findViewById(R.id.item2);
    layout.setBackgroundColor(Color.BLACK);
    text1.setText(mlist.get(position).getListitem1());
    text2.setText(mlist.get(position).getListitem2());
    return view;
}

}

答案 5 :(得分:-2)

您应该使用彩色textview进行另一项活动,并将您的列表视图与之关联起来......

list1.setAdapter(new ArrayAdapter<String>(this,R.layout.row,R.id.row_txt,item));