无法在listactivity中更改listview字体

时间:2014-10-17 06:11:12

标签: android listview android-listview

我是android编程的新手,我的任务是将我的共享首选项中的项目加载到listview,我只想更改listview的设计,比如更改其字体颜色,字体大小等。我尝试过这个解决方案,但它没有用,请告诉我哪里做错了。谢谢.. 继承我在listactivity中加载listview的代码

Map<String,?> datakeys = datapref.getAll();
    for(Map.Entry<String,?> entry : datakeys.entrySet()){
        Log.d("values123",entry.getKey() + ": " + entry.getValue().toString());    
        lvlist.add(entry.getValue().toString());}
    lvadapter  = new ArrayAdapter<String>(this, R.layout.mytextview , R.id.text1, lvlist);
    //lvadapter = new ArrayAdapter<String> (this,android.R.layout.simple_list_item_1,android.R.id.text1, lvlist);
    setListAdapter(lvadapter);

继承人mytextview xml ..

     <?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android" 
            android:id="@+id/text1"  
            android:paddingTop="2dip" 
            android:paddingBottom="3dip" 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content"
        android:textSize="10dip"
        android:textColor="#666666"
        android:textStyle="italic" />  

和我的listview xml

<ListView
    android:id="@android:id/list"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_above="@+id/button3"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/textView3"
    android:background="#b6fcd5" >

</ListView>

这是截图 enter image description here

2 个答案:

答案 0 :(得分:0)

您需要ArrayAdapter的其他构造函数:

public ArrayAdapter (Context context, int resource, int textViewResourceId, T[] objects) 

所以将此行更改为:

lvadapter = new ArrayAdapter<String>(this, R.layout.mytextview, lvlist);

致:

lvadapter  = new ArrayAdapter<String>(this, R.layout.mytextview , R.id.text1, lvlist);

mytextview xml

 <?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android" 
        android:id="@+id/text1"  
        android:paddingTop="2dip" 
        android:paddingBottom="3dip" 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content"
        android:textSize="10dip"
        android:textColor="#666666"
        android:textStyle="italic" /> 

如果这不起作用,请转到Custom Adapter

答案 1 :(得分:0)

另一种方法:看看

 String [] yourStringarrayorlist= {"Lionssss","Cats", "yeaaahhh :)"};

        ListView listView =new ListView(
                    getApplicationContext()); 
        listView.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, yourStringarrayorlist) {
                    @Override
                    public View getView(int position, View convertView, ViewGroup parent) {
                        TextView textView = (TextView) super.getView(position, convertView, parent);

                      // if you want to change any specific items color
                        int textColor = textView.getText().toString().equals("Cats") ? R.color.any : android.R.color.black;
                        textView.setTextColor(VideoviewActivity.this.getResources().getColor(textColor));

                       //If you want to change the text color of all use below code
                       // textView.setTextColor(VideoviewActivity.this.getResources().getColor( R.color.any));


                        return textView;
                    }
                });
    layoutrela.addView(listView);

您可以轻松更改文字颜色。然而,仅仅检查我已经通过代码创建了ListView并添加到布局:)但它的工作方式很棒.. 灵感来自:How to change text color of simple list item 不是一个新的答案所以我不希望任何投票左右。