利用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也是菜单,不过在头部,算是导航了 ===本文就介绍怎么制作底部菜单= ...
随机推荐
- linux系统编程----统计一个目录下的普通文件个数
主要是为了统计linux系统下一个指定目录下面的普通文件个数,运用目录操作的一些函数,配合递归调用来实现该功能. 首先介绍一下函数原型: 打开一个空目录 DIR ...
- Mac OS X 10.9 Mavericks安装后,Xcode调试时模拟器黑屏的处理方法
请耐心的等下去吧,少年! 装了Mac OS X 10.9 Mavericks的同学,如果碰到Xcode调试App时,模拟器黑屏(重置也无效),请耐心的等下去吧,大约10来分钟左右黑屏就会消失,App启 ...
- POJ3070Fibonacci(矩阵快速幂+高效)
Fibonacci Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 11587 Accepted: 8229 Descri ...
- Spynner 安装
Spynner 安装 Windows7 下安装 1.easy_install spynner 2.下载pyqt sip https://sourceforge.net/projects/pyqt/fi ...
- 将Spark中CompactBuf转换为String
val rdd = sc.textFile("hdfs://hbase11:9000/sparkTsData/ipsoftware/wincc").map{ line => ...
- rockmongo用法
.简单查询 //xid=560870 and type=video { , "type": "video" } //查询数组中的数据 array( " ...
- 用 xampp 在 windows/Linux 下搭建代理服务器
背景:学校上网开始收费,但实验室免费,由于宿舍和实验室都有ipv6,所以在实验室搭建代理服务器,让宿舍通过之上网. Windows下 两步: 1.编辑httpd.comf,去掉关于proxy的注释 L ...
- JS 下拉菜单
HTML <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <titl ...
- ASP.NET MVC学习笔记-----使用自定义的View Engine
我们都知道在ASP.NET MVC中自带了Razor View Engine,Razor十分的强大,可以满足我们绝大部分的需要.但是ASP.NET MVC的高度可扩展性,使我们可以使用自定义的View ...
- Servlet的生命周期及filter,servletRequest和servletResponse
序,Web应用中,Servlet和Filter是很重要的两个概念,一定要理解透彻. 一.Servlet类 继承自HttpServlet,HttpServlet是一个抽象类,主要包含的方法有init,s ...