实现效果:
一:创建每个选项卡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 ;
}
}
|