android中ListView的默认字体有时会知足不了设计的需求,须要设计本身的风格,android
通常网上介绍的是新建一个本身的 ListView的适配器MyAdapter,现有另外一种方法可避免新建MyAdapter的麻烦。字体
一、在res/layout/下新建 array_adapter.xml :this
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"。spa
二、在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 }
效果如图:code