TabWidget类似于通话记录的界面,通过切换多个标签从而显示出多个不同内容,能够展示内容丰富的页面信息,而且彼此之间不会干扰,有利于展示。下面,通过一个例子来学习用法

首先用一个类来继承TabActivity

在开发之前,我们要首先了解,TabHost是整个Tab的容器,包括两部分,TabWidget和FrameLayout。TabWidget就是每个tab的标签,FrameLayout则是tab内容。接着我们开始初始化main.xml。

首先声明TabHost,包含TabWidget,FrameLayout元素。

    <TabHost
android:id="@android:id/tabhost" //声明控件ID
android:layout_width="fill_parent" //控件宽度与父控件一致
android:layout_height="fill_parent"> //控件高度与父控件一致
声明TabWidget,tab标签页
<TabWidget
android:layout_width="fill_parent" //控件宽度与父控件一致
android:layout_height="wrap_content" //控件高度与自身适应
android:id="@android:id/tabs"> //声明控件ID
声明FrameLayout,tab页里的内容信息
<FrameLayout
android:layout_width="fill_parent" //控件宽度与父控件一致
android:layout_height="wrap_content" //控件高度与自身适应
android:id="@android:id/tabcontent"> //声明控件ID

注意下:

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

TabWidget必须设置android:id为@android:id/tabs

FrameLayout需要设置android:id为@android:id/tabcontent

布局文件

<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@android:id/tabhost" > <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"/>
<TabWidget android:id="@android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:visibility="gone"
/>
<RadioGroup
android:id="@+id/tab_items"
android:gravity="center_vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_gravity="bottom"
android:background="@drawable/tab_bg"
>
<RadioButton
android:id="@+id/tab_item_home"
android:checked="true"
style="@style/main_tab_bottom"
android:background="@drawable/item_home_bg" />
<RadioButton
android:id="@+id/tab_item_nearby"
style="@style/main_tab_bottom"
android:background="@drawable/item_near_bg"
/>
<RadioButton
android:id="@+id/tab_item_sort"
style="@style/main_tab_bottom"
android:background="@drawable/item_sort_bg" />
<RadioButton
android:id="@+id/tab_item_mine"
style="@style/main_tab_bottom"
android:background="@drawable/item_mine_bg"/>
<RadioButton
android:id="@+id/tab_item_more"
style="@style/main_tab_bottom"
android:background="@drawable/item_more_bg" />
</RadioGroup> </LinearLayout> </TabHost>

其中有些控件的图片点击与正常情况下是不同的,如item_home_bg.xml文件

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" > <item android:state_checked="true" android:drawable="@drawable/but_index_r_v2" />
<item android:drawable="@drawable/but_index_v2"/>
</selector>

style文件在values文件夹下的styles.xml文件中定义

<?xml version="1.0" encoding="utf-8"?>
<resources> <style name="main_tab_bottom">
<item name="android:gravity">center_horizontal</item>
<item name="android:layout_width">fill_parent</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:button">@null</item> <item name="android:layout_weight">1.0</item>
</style>
</resources>

函数实现

public class MyTab extends TabActivity{

    private final static String TAG = "TabShow";
private TabHost mHost;
private RadioGroup tabItems;
private RadioButton mineBut; @Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState); setContentView(R.layout.tablay); initResourceRefs();
initSettings();
} private void initSettings() {
// TODO Auto-generated method stub tabItems.setOnCheckedChangeListener(new OnCheckedChangeListener() { @Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
// TODO Auto-generated method stub
switch(checkedId){ case R.id.tab_item_home :
mHost.setCurrentTabByTag("HOME");
break;
case R.id.tab_item_nearby :
mHost.setCurrentTabByTag("NEAR");
break;
case R.id.tab_item_sort :
mHost.setCurrentTabByTag("SORT");
break;
case R.id.tab_item_more :
mHost.setCurrentTabByTag("MORE");
break; }
}
}); } private void initResourceRefs() {
// TODO Auto-generated method stub mHost = getTabHost(); mHost.addTab(mHost.newTabSpec("HOME").setIndicator("HOME")
.setContent(new Intent(this , HomeActivity.class))); mHost.addTab(mHost.newTabSpec("NEAR").setIndicator("NEAR")
.setContent(new Intent(this , NearByActivity.class))); mHost.addTab(mHost.newTabSpec("SORT").setIndicator("SORT")
.setContent(new Intent(this , SortActivity.class)));
mHost.addTab(mHost.newTabSpec("My").setIndicator("My")
.setContent(new Intent(this , MyActivity.class))); mHost.addTab(mHost.newTabSpec("MORE").setIndicator("MORE")
.setContent(new Intent(this , MoreActivity.class))); tabItems = (RadioGroup)findViewById(R.id.tab_items);
mineBut = (RadioButton)findViewById(R.id.tab_item_mine); } }

效果如下

利用TabWidget实现底部菜单的更多相关文章

  1. Xamarin.Android 利用Fragment实现底部菜单

    效果图: 第一步:添加引用 引用 Crosslight.Xamarin.Android.Support.v7.AppCompat 这个包. 第二步:绘制Main和Fragment界面 fg_home. ...

  2. [Android] Android 使用 FragmentTabHost + Fragment 实现 微信 底部菜单

    Android 使用 FragmentTabHost + Fragment 实现 微信 底部菜单 利用FragmentTabHost实现底部菜单,在该底部菜单中,包括了4个TabSpec,每个TabS ...

  3. 转-TabHost组件(一)(实现底部菜单导航)

    http://www.cnblogs.com/lichenwei/p/3974009.html 什么是TabHost? TabHost组件的主要功能是可以进行应用程序分类管理,例如:在用户使用wind ...

  4. 转-TabHost组件(二)(实现底部菜单导航)

    http://www.cnblogs.com/lichenwei/p/3975095.html 上面文章<安卓开发复习笔记——TabHost组件(一)(实现底部菜单导航)>中提到了利用自定 ...

  5. 安卓开发笔记——TabHost组件(二)(实现底部菜单导航)

    上面文章<安卓开发复习笔记——TabHost组件(一)(实现底部菜单导航)>中提到了利用自定义View(ImageView+TextView)来设置一个底部菜单的样式 这边再补充一种更为灵 ...

  6. 安卓开发笔记——TabHost组件(一)(实现底部菜单导航)

    什么是TabHost? TabHost组件的主要功能是可以进行应用程序分类管理,例如:在用户使用windows操作系统的时候,经常见到如图所示的图形界面.     TabHost选项卡,说到这个组件, ...

  7. Android应用主界面底部菜单实现

    介绍 现在绝大多数主流的应用主界面,都会包含一个底部菜单,就拿腾讯的QQ与微信来说,看起来是这样的  <---我是底部菜单 原理 在很久以前,可以通过TabActivity实现相关功能,自从Fr ...

  8. Vue 在手机上键盘把底部菜单顶上去的解决方案

    Vue 在手机上键盘把底部菜单顶上去的解决方案 ios和安卓的键盘的区别 ios和安卓的键盘的区别弹起方式不同, ios直接弹出键盘, 不影响页面, 而安卓键盘弹起时会把页面顶起来, 这样就会把底部菜 ...

  9. Android底部菜单的实现

    前言:以前制作菜单使用TabHost,但是android 3.0以上就被废弃了,google已经不建议使这个类了.ActionBar也是菜单,不过在头部,算是导航了 ===本文就介绍怎么制作底部菜单= ...

随机推荐

  1. sql server规范

    常见的字段类型选择 1.字符类型建议采用varchar/nvarchar数据类型 2.金额货币建议采用money数据类型 3.科学计数建议采用numeric数据类型 4.自增长标识建议采用bigint ...

  2. Common Pitfalls In Machine Learning Projects

    Common Pitfalls In Machine Learning Projects In a recent presentation, Ben Hamner described the comm ...

  3. Clion = C/C++ 和 Python 共享的 IDE

    Clion + Tdmgcc + Winpython(Python)

  4. ios开发@selector的函数如何传参数/如何传递多个参数

    不同的类会有不同的传递方式,参数名也不尽相同.如果是传单个参数的就不用集合,如果是传多个参数可以用类似nsarray,nsdictionary之类的集合传递.看下面例子: 例子1: 通过NSTimer ...

  5. visual studio 2010 破解版 破解方法

    1.Microsoft Visual Studio 2010下载(均来自微软官网) 高级版(Premium) [建议下载]       http://download.microsoft.com/do ...

  6. 新浪微博客户端(22)-创建微博Cell

    DJStatusCell.h #import <UIKit/UIKit.h> @class DJStatusCellFrame; @interface DJStatusCell : UIT ...

  7. Tools下的mdscongiguer 文件中 43行 oracle 配置 发现需要连接库 -lclntsh libclntsh.so 库是个什么东西呢?

    Tools下的mdscongiguer     文件中 43行  oracle 配置      发现需要连接库 -lclntsh      libclntsh.so 库是个什么东西呢? 分想一个知乎网 ...

  8. StreamReader和StreamWrite和FileStream区别和用法

    一.<1>StreamReader类共有10个构造函数 StreamReader (Stream)    //  为指定的流初始化 StreamReader 类的新实例. FileStre ...

  9. Java中的构造函数和重载

    一.Java中的构造函数 构造函数是对象被创建时初始化对象的成员方法,它具有和它所在的类完全一样的名字.构造函数只能有入口参数,没有返回类型,因为一个类的构造方法的返回类就是类本身.构造函数定义后,创 ...

  10. [Effective JavaScript 笔记]第23条:永远不要修改arguments对象

    arguments对象并不是标准的Array类型的实例.arguments对象不能直接调用Array方法. arguments对象的救星call方法 使得arguments可以品尝到数组方法的美味,知 ...