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中固定使用的三个函数还会在之后的文章中具体介绍。敬请期待!
tabhost实现android菜单切换的更多相关文章
- Android菜单(动画菜单、360波纹菜单)
Android菜单(动画菜单.360波纹菜单) 前言:Android菜单常用集合:FragmentTabHost系统菜单.上移式菜单.360波纹菜单.展开式菜单.详解注释,可直接拿来用! 效果: ...
- android 语言切换过程分析
android 语言切换过程分析 2014-02-27 18:13 1207人阅读 评论(0) 收藏 举报 语言切换android语言切换android改变语言 最近在看一个bug,系统切换语言后,本 ...
- cocos2dx shader实现灰度图android后台切换回来导致图像偏移的问题
转自:http://www.tuicool.com/articles/U3URRrI 项目中经常会遇到将一张图像处理成灰色的需求,为了节省资源,一般不会让美术再做一套同样的灰度图,通常会通过代码处理让 ...
- android Button 切换背景,实现动态按钮和按钮颜色渐变
android Button 切换背景,实现动态按钮和按钮颜色渐变 一.添加android 背景筛选器selector实现按钮背景改变 1.右键单击项目->new->Oth ...
- Android菜单详解(四)——使用上下文菜单ContextMenu
之前在<Android菜单详解(二)——创建并响应选项菜单>和<Android菜单详解(三)——SubMenu和IconMenu>中详细讲解了选项菜单,子菜单和图标菜单.今天接 ...
- Android菜单详解(一)——理解android中的Menu
前言 今天看了pro android 3中menu这一章,对Android的整个menu体系有了进一步的了解,故整理下笔记与大家分享. PS:强烈推荐<Pro Android 3>,是我至 ...
- 【转】Android菜单详解——理解android中的Menu--不错
原文网址:http://www.cnblogs.com/qingblog/archive/2012/06/08/2541709.html 前言 今天看了pro android 3中menu这一章,对A ...
- Android Listview切换动画,扩展到任意view切换之间动画实现
添加布局如下: <?xml version="1.0" encoding="utf-8"?> <!-- Copyright (C) 2010 ...
- Android技术——切换视图(两)随着ViewPage达到Tab幻灯片浏览
Android技术--切换视图(一)~(四)在资源项目:https://github.com/YongYuIT/MeiNv_Liulanqi 一.早期android(android.support.v ...
随机推荐
- 解决手机助手与 android sdk 的adb 冲突问题
现象:手机助手与 sdk 内的 adb冲突,用助手与真机连接后,sdk adb 就被干掉了 突发奇想: 突然有一天想到用助手的adb来覆盖sdk内的adb,果然奏效.现在eclipse.助手.cmd窗 ...
- Python基础之字符串,布尔值,整数,列表,元组,字典,集合
一.str字符串 1.capitalize字符串首字母大写 name = "json" v = name.capitalize() print(v) # 输出结果:Json 2.c ...
- Leetcode 388.文件的最长绝对路径
文件的最长绝对路径 假设我们以下述方式将我们的文件系统抽象成一个字符串: 字符串 "dir\n\tsubdir1\n\tsubdir2\n\t\tfile.ext" 表示: dir ...
- Linux中执行shell脚本命令的4种方法总结
bash shell 脚本的方法有多种,现在作个小结.假设我们编写好的shell脚本的文件名为hello.sh,文件位置在/data/shell目录中并已有执行权限. 方法一:切换到shell脚本所在 ...
- BZOJ 1016 [JSOI2008]最小生成树计数 ——Matrix-Tree定理
考虑从小往大加边,然后把所有联通块的生成树个数计算出来. 然后把他们缩成一个点,继续添加下一组. 最后乘法原理即可. 写起来很恶心 #include <queue> #include &l ...
- BZOJ2281 [SDOI2011]黑白棋 【dp + 组合数】
题目 小A和小B又想到了一个新的游戏. 这个游戏是在一个1*n的棋盘上进行的,棋盘上有k个棋子,一半是黑色,一半是白色. 最左边是白色棋子,最右边是黑色棋子,相邻的棋子颜色不同. 小A可以移动白色棋子 ...
- 【霍夫曼树】 poj 1521 Entropy
poj.org/problem?id=1521 注意只有特殊情况:只有一种字母 #include<iostream> #include<cstdio> #include< ...
- 类 this指针 const成员函数 std::string isbn() const {return bookNo;}
转载:http://www.cnblogs.com/little-sjq/p/9fed5450f45316cf35f4b1c17f2f6361.html C++ Primer 第07章 类 7.1.2 ...
- bzoj[HNOI2015]亚瑟王 - 递推与动规 - 概率与期望
[bzoj4008][HNOI2015]亚瑟王 2015年4月22日3,2991 Description 小 K 不慎被 LL 邪教洗脑了,洗脑程度深到他甚至想要从亚瑟王邪教中脱坑. 他决定,在脱坑之 ...
- gridview和detailsview的完美结合运用实现增删改
原文发布时间为:2008-07-24 -- 来源于本人的百度文章 [由搬家工具导入] 1、因Gridview中没有增加记录,所以应利用datalistview或formview来弥补。 2、因为det ...