利用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也是菜单,不过在头部,算是导航了 ===本文就介绍怎么制作底部菜单= ...
随机推荐
- stl 迭代器(了解)
STL 主要是由 containers(容器),iterators(迭代器)和 algorithms(算法)的 templates(模板)构成的. 对应于它们所支持的操作,共有五种 iterators ...
- mongodb(基础用法)
驱动和客户端库 https://mongodb-documentation.readthedocs.org/en/latest/ecosystem/drivers.html#id2 https://m ...
- 使用注解实现IOC
在biz业务处理类实现类中 /** * 用户业务类,实现对User功能的业务管理 */ @Service("userBiz") public class UserBiz imple ...
- linux基本命令(1)-用户和组管理
1.初始化Root密码 sudo passwd 2.切换至Root用户 su - root 或 sudo - i
- [Angularjs]asp.net mvc+angularjs+web api单页应用
写在前面 最近的工作一直在弄一些h5的单页应用,然后嵌入到app的webview中.之前一直在用angularjs+html+ashx的一套东西.实在是玩腻了.然后就尝试通过asp.net mvc的方 ...
- Y2K Accounting Bug(贪心)
Y2K Accounting Bug Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 10945 Accepted: 54 ...
- 小希的迷宫(MST单棵树判断法则)
小希的迷宫 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submi ...
- (9)UI(容器)
1.基础容器 基础容器可以设置子容器布局.是否裁切子元素.填充颜色.背景图片资源等属性. 使用场景. 在官方示例中,大量使用了基础容器作布局管理,如下面的主界面中,用户名称,钻石和金币就使 ...
- iPhone/iOS图片相关(读取、保存、绘制、其它相关)
http://blog.csdn.net/jerryvon/article/details/7526147 20:50:42 一.读取图片 1.从资源(resource)读取 UIImage* ima ...
- [Educational Codeforces Round 16]C. Magic Odd Square
[Educational Codeforces Round 16]C. Magic Odd Square 试题描述 Find an n × n matrix with different number ...