Cant change listview font in listactivity

泪湿孤枕 提交于 2019-12-13 03:41:58

问题


Im very new in android programming, and my task is to load up the items in my shared preference to a listview, and i just want to change the design of my listview, like changing its font color, font size and etc.. I have tried this solution but it doesn't work, please tell me where I did wrong. Thanks.. heres my code in loading the listview in a listactivity

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);

heres the 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" />  

and my 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>

here's the screenshots


回答1:


You need other constructor of ArrayAdapter :

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

so Change this line to :

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

To :

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

And 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" /> 

If this doesn't work , then Go with Custom Adapter




回答2:


one more approach: have a look

 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);

You can change the textcolor easily. However for just a check i've created ListView through code and added to the layout :) but it works like awesome.. Inspired by : How to change text color of simple list item not a new answer so i would not expect any vote or so.



来源:https://stackoverflow.com/questions/26418860/cant-change-listview-font-in-listactivity

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!