2014-10-17 133 views
0

在Android編程中,我非常新,我的任務是將我的共享首選項中的項加載到listview中,而我只是想更改我的listview的設計,如更改其字體顏色,字體大小等。我已經嘗試過這種解決方案,但它不起作用,請告訴我我做錯了什麼。謝謝.. 我的繼承人在一個listactivity不能更改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

+0

你是什麼意思*它不起作用*?你有什麼錯誤嗎? – 2014-10-17 06:13:50

+0

我沒有看到我的列表視圖中的任何更改 – ErenRavenHeart 2014-10-17 06:16:12

+0

附加屏幕截圖 – 2014-10-17 06:16:49

回答

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

如果這不起作用,th恩一起去的Custom Adapter

+0

我做了你的建議,但它仍然看起來像就像截圖一樣..沒有任何改變 – ErenRavenHeart 2014-10-17 06:45:14

+0

改變了適配器?和mytextview.xml呢? – 2014-10-17 06:48:49

+0

是的..我做了.. – ErenRavenHeart 2014-10-17 06:52:48

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 不是一個新的答案,所以我不會期望任何投票或左右。