博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
android TabHost小结
阅读量:2298 次
发布时间:2019-05-09

本文共 3184 字,大约阅读时间需要 10 分钟。

android TabHost小结
2011-06-09 11:53:07           
    

TabHost是整个Tab的容器,包括两部分,TabWidget和FrameLayout。TabWidget就是每个tab的标签,FrameLayout则是tab内容。

1、如果我们使用extends TabAcitivty,如同ListActivity,TabHost必须设置为@android:id/tabhost 

2、TabWidget必须设置android:id为@android:id/tabs 
3、FrameLayout需要设置android:id为@android:id/tabcontent 
4、参考这儿:<a href="http://blog.csdn.net/flowingflying/archive/2011/04/06/6304289.%3Ca%20href=" http:="" www.2cto.com="" kf="" web="" asp="" "="" target="_blank" class="keylink" style="color: rgb(51, 51, 51); text-decoration: none;">aspx">http://blog.csdn.net/flowingflying/archive/2011/04/06/6304289.aspx

先自定义一个xml文件: 

Java代码  
<?xml version="1.0" encoding="utf-8"?>  
<TabHost xmlns:android=""  
    android:id="@android:id/tabhost"  
    android:layout_width="fill_parent"  
    android:layout_height="fill_parent">  
    <LinearLayout  
        android:orientation="vertical"  
        android:layout_width="fill_parent"  
        android:layout_height="fill_parent">  
    <FrameLayout  
        android:id="@android:id/tabcontent"  
        android:layout_width="fill_parent"  
        android:layout_height="fill_parent"  
         android:layout_weight="1.0"  
        android:paddingBottom="53px"/>  
    <TabWidget  
        android:id="@android:id/tabs"  
        android:layout_alignParentBottom="true"  
        android:layout_width="fill_parent"  
        android:layout_height="50px"   
        android:visibility="gone"  
        android:layout_weight="0.0"/>  
        <RadioGroup  
            android:gravity="center_vertical"  
            android:orientation="horizontal"  
            android:id="@+id/main_radio"  
            android:background="@drawable/radiogroup_background"  
            android:layout_width="fill_parent"  
            android:layout_height="50dip"  
            android:layout_gravity="bottom">  
            <RadioButton  
                android:id="@+id/main_index_button"  
                android:layout_marginTop="1.0dip"  
                android:layout_marginRight="5dip"  
                android:text="@string/main_name"  
                android:drawableTop="@drawable/unistall"  
                style="@style/main_tab_bottom"  
                android:background="@drawable/radio_bg"/>  
            <RadioButton  
                android:id="@+id/main_running_button"  
                android:layout_marginTop="1.0dip"  
                android:layout_marginRight="5dip"  
                android:text="@string/run_manager_name"  
                android:drawableTop="@drawable/unistall"  
                style="@style/main_tab_bottom"  
                android:background="@drawable/radio_bg"/>  
            <RadioButton  
                android:id="@+id/main_uninstall_button"  
                android:layout_marginTop="1.0dip"  
                android:text="@string/uninstall_manager_name"  
                android:drawableTop="@drawable/unistall"  
                style="@style/main_tab_bottom"  
                android:background="@drawable/radio_bg"/>  
        </RadioGroup>  
    </LinearLayout>  
</TabHost> 

为了让tabHost显示在下方,要将RadioGroup的layout_gravity设置为bottom,再将FrameLayout的layout_weight设置为1,这样就可以将RadioGroup撑到最下方。style="@style/main_tab_bottom"里面定义了样式文件。

接下来就是在activity中初始化并添加tabhost: 

Java代码  
tabHost = (TabHost) findViewById(android.R.id.tabhost);  
        tabHost.addTab(Constant.tabHost.newTabSpec("Main")  
                .setIndicator(getString(R.string.main_name),null)  
                .setContent(new Intent(this, Main.class)));  
        tabHost.addTab(Constant.tabHost.newTabSpec("RunManager")  
                .setIndicator(getString(R.string.run_manager_name),null)  
                .setContent(new Intent(this, RunManager.class)));  
        tabHost.addTab(Constant.tabHost.newTabSpec("UninstallManager")  
                .setIndicator(getString(R.string.uninstall_manager_name),null)  
                .setContent(new Intent(this, UninstallManager.class))); 

初始化每个RadioButton并为其添加setOnCheck

分享到:
更多
您对本文章有什么意见或着疑问吗?请到 您的关注和建议是我们前行的参考和动力  
上一篇:
下一篇:
你可能感兴趣的文章
用Python实现栈
查看>>
用Python实现队列
查看>>
解释性语言和编译性语言的区别
查看>>
Python的elif语句
查看>>
迭代序列的三种方法和与序列相关的内建函数
查看>>
用Python实现求最大公约数和判断是否是素数
查看>>
用Python实现一个简单的算术游戏
查看>>
交叉编译linux内核
查看>>
awk简单用法介绍(转)
查看>>
shell脚本中使用MySQL
查看>>
linux --dup dup2 文件描述符重定向函数--输入输出重定向
查看>>
unix/linux中的dup()系统调用 --对上篇dup() dup2()例子的解释
查看>>
这学期读的书
查看>>
编译简介
查看>>
管道编程之pipe
查看>>
网络编程--C/S日期查询例子
查看>>
IPC机制---共享内存编程
查看>>
使用inotify进行文件事件通知
查看>>
Linux中时间函数的应用接口
查看>>
DNS解析过程详解
查看>>