android中TabHost和RadioGroup
在android中实现菜单功能有多种方法。
Options Menu:用户按下menu Button时显示的菜单。
Context Menu:用户长时间按下屏幕,所显示出来的菜单也称为上下文菜单。
Submenu:子菜单。
但是有时候这些内置的菜单并不能满足我们功能,这就需要自己自定义一种菜单。接下来我说的这种就是通过TabHost与RadioGroup结合完成的菜单。这也是很常用的一种底部菜单做法。先上图:

首先看布局文件:
- <?xml version="1.0" encoding="UTF-8"?>
- <TabHost android:id="@android:id/tabhost" android:layout_width="fill_parent" android:layout_height="fill_parent"
- xmlns:android="http://schemas.android.com/apk/res/android">
- <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="0.0dip"
- android:layout_weight="1.0" />
- <TabWidget
- android:id="@android:id/tabs"
- android:visibility="gone"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:layout_weight="0.0" />
- <RadioGroup
- android:gravity="center_vertical"
- android:layout_gravity="bottom"
- android:orientation="horizontal"
- android:id="@+id/main_radio"
- android:background="@drawable/maintab_toolbar_bg"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content">
- <RadioButton
- android:id="@+id/radio_button0"
- android:tag="radio_button0"
- android:layout_marginTop="2.0dip"
- android:text="@string/alarm"
- android:drawableTop="@drawable/icon_1"
- style="@style/main_tab_bottom" />
- <RadioButton
- android:id="@+id/radio_button1"
- android:tag="radio_button1"
- android:layout_marginTop="2.0dip"
- android:text="@string/message"
- android:drawableTop="@drawable/icon_2"
- style="@style/main_tab_bottom" />
- <RadioButton
- android:id="@+id/radio_button2"
- android:tag="radio_button2"
- android:layout_marginTop="2.0dip"
- android:text="@string/photo"
- android:drawableTop="@drawable/icon_3"
- style="@style/main_tab_bottom" />
- <RadioButton
- android:id="@+id/radio_button3"
- android:tag="radio_button3"
- android:layout_marginTop="2.0dip"
- android:text="@string/music"
- android:drawableTop="@drawable/icon_4"
- style="@style/main_tab_bottom" />
- <RadioButton
- android:id="@+id/radio_button4"
- android:tag="radio_button4"
- android:layout_marginTop="2.0dip"
- android:text="@string/setting"
- android:drawableTop="@drawable/icon_5"
- style="@style/main_tab_bottom" />
- </RadioGroup>
- </LinearLayout>
- </TabHost>
需要注意的是,如果用TabHost这个控件,其中有几个ID是必须这么写的,android:id="@android:id/tabhost ;android:id="@android:id/tabcontent" ;android:id="@android:id/tabs" ;之所以要这么写是因为在TabHost这个类中。需要实例化上述这个ID的控件。看源码:
在TabActivity类中有么个方法:
- @Override
- public void onContentChanged() {
- super.onContentChanged();
- mTabHost = (TabHost) findViewById(com.android.internal.R.id.tabhost);
- if (mTabHost == null) {
- throw new RuntimeException(
- "Your content must have a TabHost whose id attribute is " +
- "'android.R.id.tabhost'");
- }
- mTabHost.setup(getLocalActivityManager());
- }
当内容发生改变时它会调用这个方法,来更新列表或者其他视图,而这个方法中需要实例化TabHost,所以必须通过ID为tabhost实例化。
再看看TabHost这个类中,
- public void setup() {
- mTabWidget = (TabWidget) findViewById(com.android.internal.R.id.tabs);
- if (mTabWidget == null) {
- throw new RuntimeException(
- "Your TabHost must have a TabWidget whose id attribute is 'android.R.id.tabs'");
- }
- // KeyListener to attach to all tabs. Detects non-navigation keys
- // and relays them to the tab content.
- mTabKeyListener = new OnKeyListener() {
- public boolean onKey(View v, int keyCode, KeyEvent event) {
- switch (keyCode) {
- case KeyEvent.KEYCODE_DPAD_CENTER:
- case KeyEvent.KEYCODE_DPAD_LEFT:
- case KeyEvent.KEYCODE_DPAD_RIGHT:
- case KeyEvent.KEYCODE_DPAD_UP:
- case KeyEvent.KEYCODE_DPAD_DOWN:
- case KeyEvent.KEYCODE_ENTER:
- return false;
- }
- mTabContent.requestFocus(View.FOCUS_FORWARD);
- return mTabContent.dispatchKeyEvent(event);
- }
- };
- mTabWidget.setTabSelectionListener(new TabWidget.OnTabSelectionChanged() {
- public void onTabSelectionChanged(int tabIndex, boolean clicked) {
- setCurrentTab(tabIndex);
- if (clicked) {
- mTabContent.requestFocus(View.FOCUS_FORWARD);
- }
- }
- });
- mTabContent = (FrameLayout) findViewById(com.android.internal.R.id.tabcontent);
- if (mTabContent == null) {
- throw new RuntimeException(
- "Your TabHost must have a FrameLayout whose id attribute is "
- + "'android.R.id.tabcontent'");
- }
- }
这个方法,是在增加选项卡之前由系统调用。在这个方法中需要通过tabs 这个ID实例化一个TabWidget,通过tabcontent这个ID实例化一个FrameLayout,用来放置选项卡内容。所以这两个ID也是固定的。
在上述布局文件中隐藏了系统默认的Widget,取而代之的是带有图片的Button。
看一下主要代码:
- package com.iteye.androidtoast;
- import android.app.TabActivity;
- import android.content.Intent;
- import android.os.Bundle;
- import android.widget.RadioGroup;
- import android.widget.RadioGroup.OnCheckedChangeListener;
- import android.widget.TabHost;
- public class MainActivity extends TabActivity implements OnCheckedChangeListener{
- /** Called when the activity is first created. */
- private TabHost mHost;
- private RadioGroup radioderGroup;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.maintabs);
- //实例化TabHost
- mHost=this.getTabHost();
- //添加选项卡
- mHost.addTab(mHost.newTabSpec("ONE").setIndicator("ONE")
- .setContent(new Intent(this,OneActivity.class)));
- mHost.addTab(mHost.newTabSpec("TWO").setIndicator("TWO")
- .setContent(new Intent(this,TwoActivity.class)));
- mHost.addTab(mHost.newTabSpec("THREE").setIndicator("THREE")
- .setContent(new Intent(this,ThreeActivity.class)));
- mHost.addTab(mHost.newTabSpec("FOUR").setIndicator("FOUR")
- .setContent(new Intent(this,FourActivity.class)));
- mHost.addTab(mHost.newTabSpec("FIVE").setIndicator("FIVE")
- .setContent(new Intent(this,FiveActivity.class)));
- radioderGroup = (RadioGroup) findViewById(R.id.main_radio);
- radioderGroup.setOnCheckedChangeListener(this);
- }
- @Override
- public void onCheckedChanged(RadioGroup group, int checkedId) {
- switch(checkedId){
- case R.id.radio_button0:
- mHost.setCurrentTabByTag("ONE");
- break;
- case R.id.radio_button1:
- mHost.setCurrentTabByTag("TWO");
- break;
- case R.id.radio_button2:
- mHost.setCurrentTabByTag("THREE");
- break;
- case R.id.radio_button3:
- mHost.setCurrentTabByTag("FOUR");
- break;
- case R.id.radio_button4:
- mHost.setCurrentTabByTag("FIVE");
- break;
- }
- }
- }
android中TabHost和RadioGroup的更多相关文章
- Android中TabHost中实现标签的滚动以及一些TabHost开发的奇怪问题
最近在使用TabHost的时候遇到了一些奇怪的问题,在这里总结分享备忘一下. 首先说一点TabActivity将会被FragmentActivity所替代,但是本文中却是使用的TabActivity. ...
- Android中TabHost嵌套TabHost
在嵌套TabHost时,先后遇到了以下情况: 问题1:内部TabHos无显示,只显示了其中的一个Activity: 解决:按下文比对主子TabHos的布局文件和java文件并修改: 问题2:如上所做后 ...
- 【转】Android中dip(dp)与px之间单位转换
Android中dip(dp)与px之间单位转换 dp这个单位可能对web开发的人比较陌生,因为一般都是使用px(像素)但是,现在在开始android应用和游戏后,基本上都转换成用dp作用为单位了,因 ...
- Android中RadioGroup的初始化和简单的使用
一简介: RadioGroup作为一个单选按钮组,可以设置为性别选择男或则女,地址选择等等,作为一个android入门级选手,就简单的说一下RadioGroup组中RadioButton的布局和初始化 ...
- Android中全屏 取消标题栏,TabHost中设置NoTitleBar的三种方法(转)
Android中全屏 取消标题栏,TabHost中设置NoTitleBar的三种方法http://www.cnblogs.com/zdz8207/archive/2013/02/27/android- ...
- Android中的TabHost
TabHost是整个Tab的容器,包含TabWidget和FrameLayout两个部分,TabWidget是每个Tab的表情,FrameLayout是Tab内容. 实现方法:继承TabActivit ...
- android的tabhost+RadioGroup+PopupWindow
根据网上的代码稍作修改了下,放着记录学习. 效果图如下: 主代码如下: package com.andyidea.tabdemo; import android.app.TabActivity; im ...
- Android 中常见控件的介绍和使用
1 TextView文本框 1.1 TextView类的结构 TextView 是用于显示字符串的组件,对于用户来说就是屏幕中一块用于显示文本的区域.TextView类的层次关系如下: java.la ...
- android学习--TabHost选项卡组件
TabHost是一种非常有用的组件,TabHost能够非常方便地在窗体上放置多个标签页,每一个标签页获得了一个与外部容器同样大小的组件摆放区域.在手机系统的应用类似"未接电话".& ...
随机推荐
- jquery修改ajax的header的字段origin方法,均被浏览器拒绝
一.方法一 $.ajax({ headers: { Origin: "http://targetIP" } }); 二.方法二 $.ajax({ beforeSend: funct ...
- GSM模块_GPRS数据传输机制和原理
通信专业术语 BSS--基站子系统,通过无线接口与移动台直接联系,负责在一定区域内和移动台通信.(GSM) BTS--基站收发台,可以看作一复杂的无线调制器,BSS的主要部分,每个分配有若干信道.(G ...
- javascript数据类型检测方法
一.字符串.数字.布尔值.undefined的最佳选择市使用 typeof 运算符进行检测: 对于字符串,typeof 返回"string" 对于数字,typeof 返回" ...
- List<实体>与List<String>数据互转
1.List<实体>数据: public List<Device> queryOSDevice(String cpu,String ip,String name){ Strin ...
- scrapy-splash抓取动态数据例子十二
一.介绍 本例子用scrapy-splash通过搜狗搜索引擎,输入给定关键字抓取资讯信息. 给定关键字:数字:融合:电视 抓取信息内如下: 1.资讯标题 2.资讯链接 3.资讯时间 4.资讯来源 二. ...
- How to simplify a PHP code with the help of the façade pattern?
原文:https://phpenthusiast.com/blog/simplify-your-php-code-with-facade-class ------------------------- ...
- FBXImport
using UnityEditor; public class MyEditor : AssetPostprocessor{ public void OnPreprocessModel() { Mod ...
- 首先给大家介绍一下数据库project师,数据库project师(Database Engineer),是从事管理和维护数据库管理系统(DBMS)
摘要 MySQL的最初的核心思想,主要是开源.简便易用.其开发可追溯至1985年,而第一个内部发行版本号诞生,已经是1995年. 到1998年,MySQL已经能够支持10中操作系统了.当中就包含win ...
- Tomcat 5常用优化和配置
Tomcat 5常用优化和配置 1.JDK内存优化:Tomcat默认可以使用的内存为128MB,Windows下,在文件{tomcat_home}/bin/catalina.bat,Unix下,在文件 ...
- Leetcode:integer_to_roman
一. 题目 将给定的数字(阿拉伯数字)转化成罗马数字. 数字不会大于3999 二. 分析 首先我们要知道神马是罗马数字,尽管听说过.但事实上我还真没有记住,于是就google了下,具体 ...
