標籤:android style blog http color width
對於ArrayAdapter,裡面雖然能添加圖片,但只能是相同的圖片。
廢話不多說:
布局&&list的item布局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" > <ListView android:id="@+id/lv" android:layout_width="match_parent" android:layout_height="wrap_content" /></LinearLayout>
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal" > <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/ic_launcher" /> <TextView android:id="@+id/tv" android:layout_width="match_parent" android:layout_height="wrap_content" /></LinearLayout>
程式
private static String[] names= {"功能1","功能2","功能3","功能4","功能5"}; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ListView lv = (ListView) findViewById(R.id.lv); lv.setAdapter(new ArrayAdapter<String>(this, R.layout.list_item,R.id.tv, names)); }
--------------------------簡單的分割線--------------0-0-----------------------------------------------------------
SimpleAdapter可以添加圖片的哈~~每一個item都可以加自己想要的圖片。
在item布局裡面給imageView添加id為iv。
程式
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ListView lv = (ListView) findViewById(R.id.lv); List<Map<String,Object>> data = new ArrayList<Map<String,Object>>(); Map<String,Object> map1 = new HashMap<String, Object>(); map1.put("nameText", "第1個啦啦啦"); map1.put("icon", R.drawable.ic_launcher); Map<String,Object> map2 = new HashMap<String, Object>(); map2.put("nameText", "第2個啦啦啦"); map2.put("icon", R.drawable.info_buse); Map<String,Object> map3 = new HashMap<String, Object>(); map3.put("nameText", "第3個啦啦啦"); map3.put("icon", R.drawable.info_duijiao); Map<String,Object> map4 = new HashMap<String, Object>(); map4.put("nameText", "第4個啦啦啦"); map4.put("icon", R.drawable.info_flash); data.add(map1); data.add(map2); data.add(map3); data.add(map4); lv.setAdapter(new SimpleAdapter(this, data, R.layout.list_item, new String[]{"nameText", "icon"},new int[]{R.id.tv,R.id.iv})); }
效果:
data為繫結資料,list集合。R.layout.list_item,資料顯示對應的布局,這是要讓資料跟view對象獎勵一個映射關係。from[] ,map集合裡面的資料的key。to[],布局檔案裡面的id。
我是天王蓋地虎的分割線
原始碼:http://pan.baidu.com/s/1dD1Qx01
listview學習2.zip
轉載請註明出處:http://www.cnblogs.com/yydcdut