Android Listview文本颜色白色onCreateView(而不是黑色)

时间:2014-05-13 12:36:16

标签: java android android-listview

当应用程序启动时使用onCreateView填充列表时,附加了2张照片(白色文本),另一张是每当调用SetList()方法时

http://i.stack.imgur.com/QHNEL.png

        public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_main, container, false);
        ListView lv = (ListView) rootView.findViewById(R.id.listView1);
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(((MainActivity)getActivity()).getApplicationContext(), android.R.layout.simple_list_item_1,((MainActivity)getActivity()).Logs);
        lv.setAdapter(adapter);
        return rootView;
    }

http://i.stack.imgur.com/8uTe1.png

    public void SetList()
{
    ListView lv = (ListView) findViewById(R.id.listView1);
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,Logs);
    lv.setAdapter(adapter);
}

3 个答案:

答案 0 :(得分:6)

通过改变解决了问题 ArrayAdapter<String> adapter = new ArrayAdapter<String>(((MainActivity)getActivity()).getApplicationContext(), android.R.layout.simple_list_item_1,((MainActivity)getActivity()).Logs);

ArrayAdapter<String> adapter = new ArrayAdapter<String>(((MainActivity)getActivity()).getBaseContext(), android.R.layout.simple_list_item_1,((MainActivity)getActivity()).Logs);

似乎由于某种原因,((MainActivity)getActivity()).getApplicationContext()会返回与((MainActivity)getActivity()).getBaseContext()不同的默认文字颜色

答案 1 :(得分:0)

更改

ArrayAdapter<String> adapter = new ArrayAdapter<String>(((MainActivity)getActivity()).getApplicationContext(), android.R.layout.simple_selectable_list_item,((MainActivity)getActivity()).Logs);

ArrayAdapter<String> adapter = new ArrayAdapter<String>(((MainActivity)getActivity()).getApplicationContext(), android.R.layout.simple_list_item_1,((MainActivity)getActivity()).Logs);
onCreateView中的

。问题是您在适配器中使用了2种不同的布局。使用android.R.layout.simple_list_item_1也应该第一次为您TextView提供黑色文字。

答案 2 :(得分:0)

试试这个,覆盖getView方法并像这样设置文本颜色

 countryadapter = new ArrayAdapter<String>(CreateGroupsActivity.this,
            android.R.layout.simple_spinner_item,
            CommonListClass.countryName) {
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            // TODO Auto-generated method stub
            View v = super.getView(position, convertView, parent);
            ((TextView) v).setTextColor(getResources().getColor(R.color.black_color));
            return v;
        }

        @Override
        public View getDropDownView(int position, View convertView,
                ViewGroup parent) {
            // TODO Auto-generated method stub
            View v = super.getDropDownView(position, convertView, parent);
            ((TextView) v).setTextColor(getResources().getColor(R.color.black_color));
            return v;
        }
    };