實現效果:
一:創建每個選項卡ListView單個項內容佈局:
代碼:tab1_item.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
|
<? xml version = "1.0" encoding = "utf-8" ?> < LinearLayout xmlns:android = "http://schemas.android.com/apk/res/android" android:layout_width = "fill_parent" android:layout_height = "match_parent" android:orientation = "horizontal" >
< ImageView android:id = "@+id/image" android:layout_width = "60dp" android:layout_height = "60dp" android:padding = "10dp" />
< LinearLayout android:layout_width = "fill_parent" android:layout_height = "wrap_content" android:orientation = "vertical" >
< LinearLayout android:layout_width = "fill_parent" android:layout_height = "wrap_content" android:orientation = "horizontal" >
< TextView android:id = "@+id/text1" android:layout_width = "wrap_content" android:layout_height = "wrap_content" />
< TextView android:id = "@+id/text2" android:layout_width = "fill_parent" android:layout_height = "wrap_content" android:gravity = "right" />
</ LinearLayout > < TextView android:id = "@+id/text3" android:layout_width = "fill_parent" android:layout_height = "wrap_content" />
</ LinearLayout > </ LinearLayout > |
二:新建佈局文件tab1.xml 存放ListView組件
代碼:
1
2
3
4
5
6
7
8
9
10
11
12
|
< RelativeLayout xmlns:android = "http://schemas.android.com/apk/res/android" xmlns:tools = "http://schemas.android.com/tools" android:layout_width = "fill_parent" android:layout_height = "match_parent" >
< ListView android:id = "@+id/listview" android:layout_width = "fill_parent" android:layout_height = "match_parent" tools:context = ".MainActivity" />
</ RelativeLayout > |
三:新建android activity
Tab1Activity.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
|
public class
Tab1Activity extends
Activity { private ListView listview;
@Override public void
onCreate(Bundle savedInstanceState) { super .onCreate(savedInstanceState); setContentView(R.layout.tab1); List<Map<String, Object>> list = new
ArrayList<Map<String, Object>>(); list = getData(); SimpleAdapter adapter = new
SimpleAdapter( this , list, R.layout.tab1_item, new
String[] { "image" ,
"name" , "time" ,
"content" },
new int [] { R.id.image, R.id.text1,
R.id.text2, R.id.text3 }); listview=(ListView) this .findViewById(R.id.listview); listview.setAdapter(adapter); } private List<Map<String, Object>>
getData() { List<Map<String, Object>> list = new
ArrayList<Map<String, Object>>(); Map<String, Object> map1 = new
HashMap<String, Object>(); map1.put( "image" , R.drawable.p1); map1.put( "name" , "香香" );
map1.put( "time" , "1分鐘前" );
map1.put( "content" , "這是粉色金佛就是多幾分感慨很多功課還是客觀是個" ); list.add(map1); Map<String, Object> map2 = new
HashMap<String, Object>(); map2.put( "image" , R.drawable.p2); map2.put( "name" , "永恆" );
map2.put( "time" , "3分鐘前" );
map2.put( "content" , "今天天氣真好,心情也舒暢!!!" ); list.add(map2); Map<String, Object> map3 = new
HashMap<String, Object>(); map3.put( "image" , R.drawable.p3); map3.put( "name" , "海寶" );
map3.put( "time" , "4分鐘前" );
map3.put( "content" , "能否從開始技術規範設計的感覺開始" ); list.add(map3); Map<String, Object> map4 = new
HashMap<String, Object>(); map4.put( "image" , R.drawable.p4); map4.put( "name" , "櫻木" );
map4.put( "time" , "1小時前" );
map4.put( "content" , "而他神色間若非他嗨喲對人體打發時間通融密瑞吉斯" ); list.add(map4); Map<String, Object> map5 = new
HashMap<String, Object>(); map5.put( "image" , R.drawable.p5); map5.put( "name" , "瀟瀟" );
map5.put( "time" , "1天前" );
map5.put( "content" , "一直很高興,天天開心" );
list.add(map5); Map<String, Object> map6 = new
HashMap<String, Object>(); map6.put( "image" , R.drawable.p6); map6.put( "name" , "櫻桃" );
map6.put( "time" , "10分鐘前" );
map6.put( "content" , "sgaegeifero94eureg" ); list.add(map6); Map<String, Object> map7 = new
HashMap<String, Object>(); map7.put( "image" , R.drawable.p7); map7.put( "name" , "莉莉" );
map7.put( "time" , "2天前" );
map7.put( "content" , "每天有什麼事都說出來,這樣感覺會很輕鬆,煩惱更少、幸福更多" ); list.add(map7); return list;
} @Override public boolean
onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.activity_main, menu); return true ;
} } |
四:編寫MainActivity.java 實現選項卡與ListView結合
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
|
//繼承TabActivity public class
MainActivity extends
TabActivity { @Override public void
onCreate(Bundle savedInstanceState) { super .onCreate(savedInstanceState); // 獲取選項卡組 TabHost host = getTabHost(); /* * 創建Tab對象 創建意圖,tab選項卡與另一個activity連接, * 可以每一個選項對應一個內容,定義一個需要新建一個意圖Activity類,設置內容爲該意圖就可實現 */ Intent intent = new
Intent(); intent.setClass( this , Tab1Activity. class ); Resources r = getResources(); TabHost.TabSpectab1
= host.newTabSpec( "tab1" ) .setIndicator( "選項1" , r.getDrawable(R.drawable.p5)) .setContent(intent); TabHost.TabSpec tab2 = host.newTabSpec( "tab2" ) .setIndicator( "選項2" , r.getDrawable(R.drawable.p6)) .setContent(intent); TabHost.TabSpec tab3 = host.newTabSpec( "tab3" ) .setIndicator( "選項3" , r.getDrawable(R.drawable.p7)) .setContent(intent); // tab與選項卡組綁定 host.addTab(tab1); host.addTab(tab2); host.addTab(tab3); } @Override public boolean
onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.activity_main, menu); return true ;
} } |