简单的android选项卡Tab菜单TabHost(一)同一Activity上实现

布局代码块activity_main.xml

<?xml version="1.0" encoding="UTF-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/tabhost"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" >

         <FrameLayout 
            android:id="@android:id/tabcontent"
            android:layout_width="fill_parent"
            android:layout_height="0.0dip"
            android:layout_weight="1.0" 
                <Button
                    android:id="@+id/superman"
                    android:layout_width="wrap_content"
                    android:layout_height="fill_parent"
                    android:text="1"
                <Button
                    android:id="@+id/spiderman"
                    android:layout_width="wrap_content"
                    android:layout_height="fill_parent"
                    android:text="2"
                     />
                <Button 
                    android:id="@+id/batman"
                    android:layout_width="wrap_content"
                    android:layout_height="fill_parent"
                    android:text="3"           
                     />
    </FrameLayout>
        <TabWidget
            android:id="@android:id/tabs"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"           
            android:orientation="horizontal" />         
    </LinearLayout>  
</TabHost>

说明:(1)代码中的标签里面是选项卡当前页面的内容,如下列案例中运行效果图中的图一。标签则是选项卡的选项模块,如下列案例中运行效果图中的图二
(2)默认的选项卡模块一般是在整个界面的上面,但当设置标签里面的属性 android:layout_weight=”1.0” 的时候,选项卡模块就会自动布局在最底下如本案例。

注意:
TabHost的id必须定义为android:id=”@android:id/tabhost”,TabWidget的id必须定义为android:id=”@android:id/tabs”,FrameLayout的id必须定义为android:id=”@android:id/tabcontent”。

Activity代码块MainActivity.java

import android.os.Bundle;
import android.app.TabActivity;
import android.view.Menu;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;

@SuppressWarnings("deprecation")
public class MainActivity extends TabActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

         TabHost tabHost = getTabHost();

            TabSpec page1 = tabHost.newTabSpec("tab1")
                    .setIndicator("超人", getResources().getDrawable(R.drawable.ic_launcher))
                    .setContent(R.id.superman);
            tabHost.addTab(page1);

            TabSpec page2 = tabHost.newTabSpec("tab2")
                    .setIndicator("蜘蛛侠")
                    .setContent(R.id.spiderman);
            tabHost.addTab(page2);

            TabSpec page3 = tabHost.newTabSpec("tab3")
                    .setIndicator("蝙蝠侠")
                    .setContent(R.id.batman);
            tabHost.addTab(page3);
    }
}

运行效果如下:

内容
图一
选项卡模块
图二
运行效果图
运行效果图

源代码下载

TabMenu.jar

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值