android极简原创系列:tabhost最简单配置

第一步配置布局文件


01<TabHost
02        android1:id="@android:id/tabhost"
03        android1:layout_width="match_parent"
04        android1:layout_height="match_parent"
05        android1:layout_weight="1" >
06 
07        <LinearLayout
08            android1:layout_width="match_parent"
09            android1:layout_height="match_parent"
10            android1:orientation="vertical" >
11 
12            <TabWidget
13                android1:id="@android:id/tabs"
14                android1:layout_width="match_parent"
15                android1:layout_height="wrap_content" >
16            </TabWidget>
17 
18            <FrameLayout
19                android1:id="@android:id/tabcontent"
20                android1:layout_width="match_parent"
21                android1:layout_height="match_parent" >
22 
23                <LinearLayout
24                    android1:id="@+id/tab1"
25                    android1:layout_width="match_parent"
26                    android1:layout_height="match_parent" >
27 
28                    <ListView
29                        android1:id="@+id/listview1"
30                        android1:layout_width="fill_parent"
31                        android1:layout_height="fill_parent" >
32                    </ListView>
33                </LinearLayout>
34 
35                <LinearLayout
36                    android1:id="@+id/tab2"
37                    android1:layout_width="match_parent"
38                    android1:layout_height="match_parent" >
39                </LinearLayout>
40            </FrameLayout>
41        </LinearLayout>
42    </TabHost>
上面这些大部分照抄即可,关键点在
1<LinearLayout
2                    android1:id="@+id/tab2"
3                    android1:layout_width="match_parent"
4                    android1:layout_height="match_parent" >
5                </LinearLayout>


在linearlayout中设置你需要的控件,我这里放了一个listview

第二步 配置activity

1final TabHost tabhost = (TabHost) findViewById(android.R.id.tabhost);
2        tabhost.setup();
3        TabWidget tabwidget = tabhost.getTabWidget();
4        tabhost.addTab(tabhost.newTabSpec("tab1").setIndicator("tab1")
5                .setContent(R.id.tab1));
6        tabhost.addTab(tabhost.newTabSpec("tab2").setIndicator("tab2")
7                .setContent(R.id.tab2));
我这里新建了两个tab,如果要继续增加根据上面的规律添加即可!

第三步 事件监听

01tabhost.setOnTabChangedListener(new OnTabChangeListener() {
02         public void onTabChanged(String arg0) {
03         // TODO Auto-generated method stub
04         Toast.makeText(DiquActivity.this, "当前标签页为" + arg0,
05         Toast.LENGTH_SHORT).show();
06         }
07         });
08 
09        tabhost.getTabWidget().getChildAt(0)
10                .setOnClickListener(new OnClickListener() {
11 
12                    @Override
13                    public void onClick(View v) {
14                        // TODO Auto-generated method stub
15                        tabhost.setCurrentTab(0);
16                        Toast.makeText(DiquActivity.this, "当前标签页为1",
17                                Toast.LENGTH_SHORT).show();
18                    }
19                });
20 
21        tabhost.getTabWidget().getChildAt(1)
22                .setOnClickListener(new OnClickListener() {
23 
24                    @Override
25                    public void onClick(View v) {
26                        // TODO Auto-generated method stub
27                        tabhost.setCurrentTab(1);
28                        Toast.makeText(DiquActivity.this, "当前标签页为2",
29                                Toast.LENGTH_SHORT).show();
30                    }
31                });
很容易懂吧,我就不解释了。照着做即可。android4.0环境下通过。
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值