tabhost实现android菜单切换

做APP项目已经有半个月了。慢慢地熟悉了这个开发环境和开发套路。

虽然是摸着石头过河。但也渐渐看到了水的深度!

作为一个电商项目APP,势必会涉及究竟部菜单条的功能。自己实现这个功能的过程是崎岖的,最总完毕之后才发现这样的崎岖对于自己的学习是非常有帮助的!

对于这部分的探索拿来和大家分享,希望能够相助于大家!

实现app底部菜单条的方法有非常多种,亲身尝试了tabhost和fragment两种方式,终于还是成功做成了tabhost,拿来和大家分享。

事实上tabhost实现底部菜单条的功能非常easy。真正的高手已经给我们建立了基础。须要我们做的就是熟悉并运用好它就可以实现我们要的功能。所以我们就非常有必要认真地研究一下abhost内部到底有什么好东西。它又是怎么帮助我们实现的页面切换。

首先让我们来认识一下tabhost中很重要的一些函数indicator(指示器)、content和tag.一个tab通常包括了indiicator(指示器)、content(tab相应展示的页面view。能够为这个view的id或者是一个intent)、tag。

当中TabSpec就是用来辅助设置这些选项:

<1> Indicator一般是设置tab的名称label和icon;

<2> Content:能够是一个view的id,也能够通过TabContentFactory创建一个view,还能够是一个Intent组件来载入一个activity。

用到的java类代码:

<span style="font-family:KaiTi_GB2312;">package jczb.shoping.ui;

import android.app.Activity;
import android.app.TabActivity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TabHost;
import android.widget.TextView;
import android.widget.TabHost.TabSpec;

public class MenuActivity extends TabActivity implements OnClickListener{

	LinearLayout llchannel1,llchannel2,llchannel3,llchannel4;
	ImageView ivMain,ivCart,ivMine;
	Intent itMain,itCart,itMine;
	int mCurTabId = R.id.channel1;

	//定义tab的四个button
	public static String TAB_TAG_ANBIAOCHECK="anbiaocheck";
	public static String TAB_TAG_SETTINGS="settings";
	public static String TAB_TAG_DOWNLOAD="download";

	public static TabHost mTabHost;

	private Animation left_in,left_out;
	private Animation right_in,right_out;

	@SuppressWarnings("deprecation")
	@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_menu);
        prepareAnim();
		prepareIntent();
		setupIntent();
		findViewById();
		initView();

    }

	/*依据ID拿到控件*/
	protected void findViewById() {
		ivMain = (ImageView)findViewById(R.id.imageView1);
		ivCart = (ImageView)findViewById(R.id.imageView2);
		ivMine = (ImageView)findViewById(R.id.imageView3);
		llchannel1 = (LinearLayout)findViewById(R.id.channel1);
		llchannel2 = (LinearLayout)findViewById(R.id.channel2);
		llchannel3 = (LinearLayout)findViewById(R.id.channel3);

	}

	/**
	 * 动画效果
	 */
	private void prepareAnim() {

		left_in = AnimationUtils.loadAnimation(this, R.anim.left_in);
		left_out = AnimationUtils.loadAnimation(this, R.anim.left_out);

		right_in = AnimationUtils.loadAnimation(this, R.anim.right_in);
		right_out = AnimationUtils.loadAnimation(this, R.anim.right_out);
	}

	/**
	 * Intent跳转信息
	 */
	private void prepareIntent(){
		//首页
		itMain = new Intent(this,MainActivity.class);
		//购物车
		itCart = new Intent(this,CartActivity.class);
		//我的
		itMine = new Intent(this,MineActivity.class);

	}

	/*供setupIntent调用
	 * 标记tag、载入图片,切换页面
	 * */
	private TabHost.TabSpec buildTabSpec(String tag, int resLabel, int resIcon,
			final Intent content) {
		return mTabHost
				.newTabSpec(tag)
				.setIndicator(getString(resLabel),
						getResources().getDrawable(resIcon))
				.setContent(content);
	}

	public static void setCurrentTabByTag(String tab) {
		mTabHost.setCurrentTabByTag(tab);
	}

	/**
	 * 获取Tabhost
	 */
	private  void setupIntent() {
		mTabHost = getTabHost();
		mTabHost.addTab(buildTabSpec(TAB_TAG_ANBIAOCHECK, R.string.securitycheck_btn_str,
				R.drawable.main_bottom_tab_home_normal, itMain));
		mTabHost.addTab(buildTabSpec(TAB_TAG_SETTINGS,R.string.securitycheck_btn_str,
				R.drawable.main_bottom_tab_cart_focus, itCart));
		mTabHost.addTab(buildTabSpec(TAB_TAG_DOWNLOAD, R.string.securitycheck_btn_str,
				R.drawable.main_bottom_tab_personal_focus, itMine));

	}

	/*单击事件监听器*/
	protected void initView() {
		llchannel1.setOnClickListener(this);
		llchannel2.setOnClickListener(this);
		llchannel3.setOnClickListener(this);
	}

	@Override
	public boolean onKeyDown(int keyCode,KeyEvent event){
		if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
			ivMain.performClick();
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}

	/*响应单位价格事件、改变button图片*/
	@Override
	public void onClick(View v){
		if (mCurTabId == v.getId()) {
			return;
		}

		int checkedId = v.getId();
		final boolean o;
		if (mCurTabId < checkedId) {
			o = true;
		} else {
			o = false;
		}

		if (o) {
			mTabHost.getCurrentView().startAnimation(left_out);
		} else {
			mTabHost.getCurrentView().startAnimation(right_out);
		}
		switch (checkedId) {
		//实现相似于各个子tabhost点击之后图片切换的效果
		case R.id.channel1:
			mTabHost.setCurrentTabByTag(TAB_TAG_ANBIAOCHECK);
			ivMine.setImageResource(R.drawable.main_bottom_tab_personal_focus);
			ivCart.setImageResource(R.drawable.main_bottom_tab_cart_focus);
			ivMain.setImageResource(R.drawable.main_bottom_tab_home_normal);
			break;
		case R.id.channel2:
			mTabHost.setCurrentTabByTag(TAB_TAG_SETTINGS);
			ivMine.setImageResource(R.drawable.main_bottom_tab_personal_focus);
			ivCart.setImageResource(R.drawable.main_bottom_tab_cart_normal);
			ivMain.setImageResource(R.drawable.main_bottom_tab_home_focus);
			break;
		case R.id.channel3:
			mTabHost.setCurrentTabByTag(TAB_TAG_DOWNLOAD);
			ivMine.setImageResource(R.drawable.main_bottom_tab_personal_normal);
			ivCart.setImageResource(R.drawable.main_bottom_tab_cart_focus);
			ivMain.setImageResource(R.drawable.main_bottom_tab_home_focus);
			break;
		default:
			break;
		}
		if (o) {
			mTabHost.getCurrentView().startAnimation(left_in);
		}else {
			mTabHost.getCurrentView().startAnimation(right_in);
		}
		mCurTabId = checkedId;
	}

}
</span>

页面的xml代码;

<span style="font-family:KaiTi_GB2312;"><?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/tabhost"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@color/black"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="@color/white"
        android:orientation="vertical" >

        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="1" >
        </FrameLayout>

        <TabWidget
            android:id="@android:id/tabs"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="#000000"
            android:visibility="gone" />

        <LinearLayout
	            android:layout_width="fill_parent"
	            android:layout_height="wrap_content"
	            android:layout_alignParentBottom="true"
	            android:background="@color/whitesmoke" >

		            <!-- 主页 -->
		            <LinearLayout
		                android:id="@+id/channel1"
		                style="@style/main_tab_but_linear" >

		                <ImageView
		                    android:id="@+id/imageView1"
		                    android:layout_width="wrap_content"
		                    android:layout_height="wrap_content"
		                    android:layout_marginTop="4dp"
		                    android:layout_weight="1"
		                    android:src="@drawable/main_bottom_tab_home_normal" />
		             </LinearLayout>   

		            <!-- 购物车 -->
		            <LinearLayout
		                android:id="@+id/channel2"
		                style="@style/main_tab_but_linear" >
		                <ImageView
		                    android:id="@+id/imageView2"
		                    android:layout_width="wrap_content"
		                    android:layout_height="wrap_content"
		                    android:layout_marginTop="4dp"
		                    android:layout_weight="1"
		                    android:src="@drawable/main_bottom_tab_cart_focus"/>
		            </LinearLayout>

		            <!-- 我的-->
		            <LinearLayout
		                android:id="@+id/channel3"
		                style="@style/main_tab_but_linear"
		                android:layout_width="fill_parent"
		                android:layout_height="fill_parent"
		                android:layout_weight="1"
		                android:orientation="vertical" >
		                <ImageView
		                    android:id="@+id/imageView3"
		                    android:layout_width="wrap_content"
		                    android:layout_height="wrap_content"
		                    android:layout_marginTop="4dp"
		                    android:layout_weight="1"
		                    android:src="@drawable/main_bottom_tab_personal_focus" />
		            </LinearLayout>
	      </LinearLayout>

    </LinearLayout>

</TabHost>
</span>

注意:

代码中设计到了其它跳转页面的xml。须要的话自己建立空白的xml页面就可以!

虽然实现了须要的功能,可是对于tabhost的理解还有待提高。对于tabhost中固定使用的三个函数还会在之后的文章中具体介绍。敬请期待!

时间: 2024-08-06 05:11:09

tabhost实现android菜单切换的相关文章

Android菜单详解(一)——理解android中的Menu

前言 今天看了pro android 3中menu这一章,对Android的整个menu体系有了进一步的了解,故整理下笔记与大家分享. PS:强烈推荐<Pro Android 3>,是我至今为止看到的最好的一本android书,中文版出到<精通Android 2>. 理解Android的菜单 菜单是许多应用程序不可或缺的一部分,Android中更是如此,所有搭载Android系统的手机甚至都要有一个"Menu"键,由此可见菜单在Android程序中的特殊性.An

android 语言切换过程分析 (zhuan)

最近在看一个bug,系统切换语言后,本来退到后台的音乐,会在通知栏上显示通知.为了解决这个bug,我学习了下android的语言切换流程,也参考了大量其他人的资料.(主要参考了http://blog.csdn.net/wqhjfree/article/details/8244520)在这里我将自己的探索记录下来,作为自己的学习记录,也希望能对有同样需要的人有个帮助.刚学android不久,如果中间有什么问题不对的请多多谅解,并指出错误,多交流,共同进步下. 1.从setting入手,我们可以知道

js实现菜单切换

小案例:实现菜单栏的切换,点击左侧边栏,展示右侧主体的页面 首先实现html页面的编写: <div id="header"></div> <div id="main"> <!--左侧边栏--> <div class="affix"> <h4>用户中心</h4> <ul> <li><a href="#container-myo

Android菜单简析02(ContextMenu)

在上一篇文章 Android 菜单简析01(OptionsMenu) 中给大家介绍了OptionsMenu 的使用,这篇接着给大家介绍下ContextMenu 的用法. ContextMenu 简称上下文菜单,通过长按事件响应,有两种响应模式 浮动模式 效果类似弹出的 Dialog,在屏幕的正中央,可以自定义显示的 Menu 以及 MenuItem 的响应 动作模式 通过ActionBar 实现,效果实在 Title 的地方,出现动作条. 特别注意 ContextMenu 的 动作模式 在 An

android:id=&quot;@android:id/tabhost&quot; 、android:id=&quot;@+id/llRoot&quot; 、android:id=&quot;@id/llRoot&quot; 之间的区别

由于快要放暑假了,所以最近这俩周把Android方面的知识复习一下,准备找个实习工作. 顺便把自己的总结更大家分享一下,共同进步,谢谢.... 一. android:id="@android:id/tabhost"   是调用系统内部的ID 和代码中 mTabContent = (FrameLayout) findViewById(com.android.internal.R.id.tabcontent); 是一回事. 二. android:id="@+id/llRoot&q

android 菜单的总结

安卓菜单有三种菜单. 选项菜单: 点击系统菜单按钮会触发 上下文菜单:长按屏幕触发 子菜单:某一个菜单的下一级菜单 具体的描叙:http://blog.csdn.net/zqiang_55/article/details/7038085 写的简洁易懂.呵呵,其他的都太复杂了 我参考这个思路 写了一个演示代码. package cn.wuwenfu.menudemo; import com.example.menudemo.R; import android.app.Activity; impor

Android的TabHost组件-android的学习之旅(四十)

TabHost简介 虽然,官方建议用Fagment取代TabHost,但是我们还是大概的介绍一下.TabHost是一种非常简单的组件,TabHost可以很方便的在窗口放置多个标签页,每一个标签页相当于获得了一个摆放位置. 注意 TabHost的内部需要两个组件一个是TabWidget和FrameLayout两个组件. 通话记录界面 <?xml version="1.0" encoding="utf-8"?> <TabHost xmlns:andro

Unity5_UGUI基本菜单切换学习

UGUI基本菜单切换学习 首先先拖放好两个Panel,并且在两个Panel里面各放一个Button 分别改名为 PanelAA , PanelBB 按钮名字改为 myButton ,targetButton 新建一个空的Gameobject ,改名为 UIchange 如图 新建C#脚本 UIClickeds.cs 代码如下 using UnityEngine; using System.Collections; public class UIClickeds : MonoBehaviour {

cocos2dx shader实现灰度图android后台切换回来导致图像偏移的问题

转自:http://www.tuicool.com/articles/U3URRrI 项目中经常会遇到将一张图像处理成灰色的需求,为了节省资源,一般不会让美术再做一套同样的灰度图,通常会通过代码处理让图片变灰.网上也有很多用shader处理图片变灰的方法,这些方法确实也实现了让图片变灰的需求,但是android平台从后台切换回来的时候,shader被释放,导致图片位置错乱.关键在于从android后台切换回来的时候需要重新加载shader.我们看一下cocos2dx原生的shader处理方式,我