利用TabWidget实现底部菜单
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实现底部菜单的更多相关文章
- Xamarin.Android 利用Fragment实现底部菜单
效果图: 第一步:添加引用 引用 Crosslight.Xamarin.Android.Support.v7.AppCompat 这个包. 第二步:绘制Main和Fragment界面 fg_home. ...
- [Android] Android 使用 FragmentTabHost + Fragment 实现 微信 底部菜单
Android 使用 FragmentTabHost + Fragment 实现 微信 底部菜单 利用FragmentTabHost实现底部菜单,在该底部菜单中,包括了4个TabSpec,每个TabS ...
- 转-TabHost组件(一)(实现底部菜单导航)
http://www.cnblogs.com/lichenwei/p/3974009.html 什么是TabHost? TabHost组件的主要功能是可以进行应用程序分类管理,例如:在用户使用wind ...
- 转-TabHost组件(二)(实现底部菜单导航)
http://www.cnblogs.com/lichenwei/p/3975095.html 上面文章<安卓开发复习笔记——TabHost组件(一)(实现底部菜单导航)>中提到了利用自定 ...
- 安卓开发笔记——TabHost组件(二)(实现底部菜单导航)
上面文章<安卓开发复习笔记——TabHost组件(一)(实现底部菜单导航)>中提到了利用自定义View(ImageView+TextView)来设置一个底部菜单的样式 这边再补充一种更为灵 ...
- 安卓开发笔记——TabHost组件(一)(实现底部菜单导航)
什么是TabHost? TabHost组件的主要功能是可以进行应用程序分类管理,例如:在用户使用windows操作系统的时候,经常见到如图所示的图形界面. TabHost选项卡,说到这个组件, ...
- Android应用主界面底部菜单实现
介绍 现在绝大多数主流的应用主界面,都会包含一个底部菜单,就拿腾讯的QQ与微信来说,看起来是这样的 <---我是底部菜单 原理 在很久以前,可以通过TabActivity实现相关功能,自从Fr ...
- Vue 在手机上键盘把底部菜单顶上去的解决方案
Vue 在手机上键盘把底部菜单顶上去的解决方案 ios和安卓的键盘的区别 ios和安卓的键盘的区别弹起方式不同, ios直接弹出键盘, 不影响页面, 而安卓键盘弹起时会把页面顶起来, 这样就会把底部菜 ...
- Android底部菜单的实现
前言:以前制作菜单使用TabHost,但是android 3.0以上就被废弃了,google已经不建议使这个类了.ActionBar也是菜单,不过在头部,算是导航了 ===本文就介绍怎么制作底部菜单= ...
随机推荐
- groovy-位运算
从Groovy 1.0 beta 10开始,Groovy支持位运算:<<. >>, >>>, |, &, ^, and ~. 下表列出了位运算的操作符 ...
- vagrant up时提示 Authentication failure. Retrying
vagrant up时提示 Authentication failure. Retrying 如图,启动时就报这个错误,virtualbox启动正常 用vagrant的账号密码也可以登录 就是不能使用 ...
- 织梦DedeCms网站更换域名后文章图片路径批量修改
因为织梦上传图片用的是绝对地址,如果域名更换后,之前发布的文章的图片URL是不会跟着改变的,所以我们需要把旧域名替换成新的域名,方法很简单,有一段SQL语句更新一下文章正文内容就行. 复制下面SQL语 ...
- ALTER 语句修改数据表
1.修改数据表名:alter table 表名 rename 新表名; 2.修改列名: alter table 表名 change 列名 新列名(可以与旧的一样) 类型 默认值; 3.修改类型: al ...
- B/S C/S架构的界面测试
网站是B/S架构的典型,从做网站的有限经验来整理一下B/S测试的基本要点,并把它与C/S进行区分. 与C/S相比,以下4个测试是除了常用测试外还要注意的: (1)链接测试 (2)表单测试 (3)脚本测 ...
- windows下安装redis以及简单的事例
1.安装服务端下载地址:http://code.google.com/p/servicestack/wiki/RedisWindowsDownload我下载了一个 redis-2.0.0服务器包,解压 ...
- [原] Android持续优化 - 提高流畅度
一.形象的感官一下流畅度概念 1. 这是官方给出的概念:Android流畅运行,需要运行60帧/秒, 则需要每帧的处理时间不超过16ms. 2. 每秒帧数,实际上就是指动画或视频每秒放映的画面数.因此 ...
- HDOJ 2089 不要62
不要62 Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submis ...
- Swift Tour 随笔总结 (3)
关于Optional的Control Flow if let constantName = someOptional { statements } 如果该Optional为nil,则不进入if,否则执 ...
- Linux 怎么把自己写的脚本添加到服务里面,即可以使用service命令来调用
chmod 755 filename; mv filename /etc/init.d/; chkconfig --add filename #!/bin/bash #chkconfig: 345 8 ...