修改 android ListView 字體大小及顏色

android中ListView的默認字體有時會滿足不了設計的需求,需要設計自己的風格,

一般網上介紹的是新建一個自己的 ListView的適配器MyAdapter,現有另一種方法可避免新建MyAdapter的麻煩。

1、在res/layout/下新建 array_adapter.xml :

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <TextView xmlns:android="http://schemas.android.com/apk/res/android"
 3     android:id="@+id/TextView"
 4     android:layout_width="wrap_content"
 5     android:layout_height="wrap_content"
 6     android:textSize="24sp"
 7     android:padding="10dp" 
 8     android:textColor="#ff000000"   
 9     android:shadowDx="4"
10     android:shadowDy="4"
11     android:shadowRadius="2"
12  />

其中"android:textSize"及"android:textColor"就可以指定字體大小及顏色,當然也可以進行其他的設置如添加陰影等"android:shadowColor"。

 

2、在MainActivity中創建適配器,並將array_adapter.xml與適配器想關聯:

 1 private ListView    m_logList=null;
 2 private ArrayAdapter adapter = null;
 3 private ArrayList<String> m_logTexts;
 4 
 5 public void onCreate(Bundle savedInstanceState) {
 6     super.onCreate(savedInstanceState);
 7     // .. .. ..
 8 
 9     m_logList = (ListView)this.findViewById(R.id.listView1);
10     m_logTexts=new ArrayList<String>();
11     m_logTexts.add("Hello World.");
12     m_logTexts.add("11 22 33");
13 
14     // 創建適配器,並把數據交給適配器  
15     adapter = new ArrayAdapter(this,R.layout.array_adapter,m_logTexts);        
16     // 爲listView添加適配器  
17     m_logList.setAdapter(adapter);
18     
19 }
20 
21 public void onLog(String str) {
22 
23     while(m_logTexts.size()>15)
24     {
25         m_logTexts.remove(0);
26     }
27     m_logTexts.add(str);
28 
29         //  通知顯示
30     adapter.notifyDataSetChanged();
31 }


效果如圖:

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章