自定义 AdapterView
场景:自定义 AdapterView 格局
自定义 AdapterView 布局
自定义 AdapterView 布局
何谓 AdapterView 以前有说过 就是如下几个View:
写道
ListView
Gallery
GridView
Spinner
Gallery
GridView
Spinner
至于布局 系统已经为我们定义了一下 比如:
android.R.layout.simple_list_item_1 android.R.layout.simple_list_item_2 android.R.layout.simple_list_item_3 ...
但是系统肯定不能满足我们的需要 所以有时候我们得根据界面的需要自定义 这有2种方法:
1. 自定义BaseAdapter 并实现其 getView()
但是 有时候 我们要显示的数据源也是其支持的TextView 自定义BaseAdapter 好像有点舍本逐末了 得不偿失
2. 自定义界面 然后在Adapter中使用之
写道
Adapter 显示中 每一行就是一个View 你可以把这个View 定义成任何子布局
* 现在有程序要求显示3个TextView 假设该布局如下:simple_adapter_g3.xml
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content" android:stretchColumns="0" android:padding="5dp"> <LinearLayout android:orientation="horizontal" > <TextView android:id="@+id/text1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="16sp" android:textColor="#FFC8FF" android:textStyle="bold|italic" /> <TextView android:id="@+id/text3" android:layout_width="wrap_content" android:layout_height="fill_parent" android:textSize="16sp" android:layout_marginLeft="50px" /> </LinearLayout> <TextView android:id="@+id/text2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="16sp" android:textStyle="bold"/> </TableLayout>
* 如何使用
String[] from={COLUMN_1,COLUMN_2,COLUMN_3}; int[] to={R.id.text1,R.id.text2, R.id.text3}; SimpleAdapter adapter = new SimpleAdapter(this,people,R.layout.simple_adapter_g3,from,to);
* emulator 运行截图为: