需求:使用TabHost实现底部菜单栏;

效果图:

实现分析:

1.目录结构:

代码实现:

1.activity_main.xml

<?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="match_parent"
android:layout_height="match_parent" > <LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" > <FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" /> <TabWidget
android:id="@android:id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="2dp"
android:layout_weight="0"
android:background="@drawable/tab_widget_background" />
</LinearLayout> </TabHost>

2 activity_one.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/image_01" > </FrameLayout>

3 item_tab_view.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="vertical" > <ImageView
android:id="@+id/image_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="3dp" /> <TextView
android:id="@+id/text_name"
style="@style/item_tab_text_style"
android:layout_width="wrap_content"
android:layout_height="wrap_content" /> </LinearLayout>

4 tab_background_selector.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:drawable="@drawable/tab_item_p" android:state_pressed="true"/>
<item android:drawable="@drawable/tab_item_d" android:state_selected="true"/> </selector>

5 style.xml

    <style name="item_tab_text_style">
<item name="android:textSize">10.0dip</item>
<item name="android:textColor">#ffffffff</item>
<item name="android:ellipsize">marquee</item>
<item name="android:singleLine">true</item>
</style>

6 Constant.java

package com.jjc.demo;

/**
* @author ThinkPad
* 功能描述:常量工具类
*/
public class Constant { public static final class ConValue{ /**
* Tab选项卡的图标
*/
public static int mImageViewArray[] = {
R.drawable.tab_icon1,
R.drawable.tab_icon2,
R.drawable.tab_icon3,
R.drawable.tab_icon4,
R.drawable.tab_icon5
}; /**
* Tab选项卡的文字
*/
public static String mTextViewArray[] = {"主页", "关于", "设置", "搜索", "更多"}; /**
* 每一个Tab界面
*/
public static Class<?> mTabClassArray[] = {
ActivityOne.class,
ActivityTwo.class,
ActivityThree.class,
ActivityFour.class,
ActivityFive.class
};
}
}

7 ActivityOne.java

package com.jjc.demo;

import android.app.Activity;
import android.os.Bundle; public class ActivityOne extends Activity{ @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_one);
}
}

8 MainActivity.java

package com.jjc.demo;

import com.jjc.demo.Constant.ConValue;

import android.app.TabActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;
import android.widget.TextView; public class MainActivity extends TabActivity { private TabHost mTabHost;
private LayoutInflater mInflater; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); initView();
} /**
* 初始化组件
*/
private void initView() {
// 实例化TabHost对象,得到TabHost
mTabHost = getTabHost(); // 实例化布局对象
mInflater = LayoutInflater.from(this); // 得到Activity的个数
int count = ConValue.mTabClassArray.length; for (int i = 0; i < count; i++) {
// 为每一个Tab按钮设置图标、文字和内容
TabSpec tabSpec = mTabHost.newTabSpec(ConValue.mTextViewArray[i])
.setIndicator(getTabItemView(i))
.setContent(getTabItemIntent(i));
// 将Tab按钮添加进Tab选项卡中
mTabHost.addTab(tabSpec);
// 设置Tab按钮的背景
mTabHost.getTabWidget().getChildAt(i)
.setBackgroundResource(R.drawable.tab_background_selector);
}
} /**
* 给Tab按钮设置图标和文字
*/
private View getTabItemView(int index) {
View view = mInflater.inflate(R.layout.item_tab_view, null);
ImageView imageView = (ImageView) view.findViewById(R.id.image_icon);
if (imageView != null) {
imageView.setImageResource(ConValue.mImageViewArray[index]);
}
TextView textView = (TextView) view.findViewById(R.id.text_name);
textView.setText(ConValue.mTextViewArray[index]);
return view;
} /**
* 给Tab选项卡设置内容(每个内容是一个Activity)
*/
private Intent getTabItemIntent(int index) {
Intent intent = new Intent(this, ConValue.mTabClassArray[index]);
return intent;
}
}

代码:http://pan.baidu.com/s/1lLFx8

底部菜单栏(一) TabHost实现的更多相关文章

  1. 完美逆向百度手机助手5.0底部菜单栏 - Android Tabhost 点击动画

    先看看百度手机助手5.0的样子: 发现他是用一个CustomTabHost.java来实现底部TabHost点击效果的,很漂亮,点击Tab的时候文字会上跑,图片会从底部跑出来的一个小动画. 下面我用自 ...

  2. 底部菜单栏(二) TabHost & RadioGroup 实现

    需求:使用TabHost & RadioGroup实现底部菜单栏: 效果图: 实现分析: 1.目录结构: 代码实现: 1. activity_main.xml <?xml version ...

  3. 【Android UI设计与开发】5.底部菜单栏(二)使用Fragment实现底部菜单栏

    既然 Fragment 取代了TabActivity,当然 TabActivity 的能实现的菜单栏,Fragment 当然也能实现.主要其实就是通过菜单栏的点击事件切换 Fragment 的显示和隐 ...

  4. FragmentTabHost+FrameLayout实现底部菜单栏

    现在一般的app都使用底部菜单栏,那具体怎么实现的呢!我们就来看看 首先给大家展示一下布局文件 1 <LinearLayout xmlns:android="http://schema ...

  5. 【Android开发笔记】底部菜单栏 FragmentTabHost

    公司项目,需求本来是按照谷歌官方指南写的,菜单栏设计成在导航栏下方 结果呢,审评时,BOSS为了和iOS统一,改成了底部菜单栏(标准结局),我只能呵呵呵呵呵呵呵 查了查资料发现实现底部菜单栏用的是Fr ...

  6. 我的Android之路——底部菜单栏的实现

    底部菜单栏的实现 底部菜单栏两种实现方法:ViewPager:可滑动的界面:Fragment:固定的界面. 首先,页面布局,在除去顶部toolbar之后,将主界面分为两部分,一部分为界面显示区,另一部 ...

  7. Android底部菜单栏+顶部菜单

    底部菜单栏+顶部菜单(wechat)demo http://blog.csdn.net/evankaka/article/details/44121457 底部菜单demo http://blog.c ...

  8. android 底部菜单栏实现(转)

    1.Android学习之BottomNavigationBar实现Android特色底部导航栏 2.Android底部导航栏的四种实现 3.Android BottomNavigationBar底部导 ...

  9. 【Android UI设计与开发】7.底部菜单栏(四)PopupWindow 实现显示仿腾讯新闻底部弹出菜单

    前一篇文章中有用到 PopupWindow 来实现弹窗的功能.简单介绍以下吧. 官方文档是这样解释的:这就是一个弹出窗口,可以用来显示一个任意视图.出现的弹出窗口是一个浮动容器的当前活动. 1.首先来 ...

随机推荐

  1. httpsClient

    Http协议的重要性相信不用我多说了,HttpClient相比传统JDK自带的URLConnection,增加了易用性和灵活性(具体区别,日后我们再讨论),它不仅是客户端发送Http请求变得容易,而且 ...

  2. Unity3D WP8发布解决方案名 DefaultPackageName 修改问题

    原地址:http://blog.csdn.net/w337198302/article/details/16960661 在对Unity3D游戏进行Windows phone 8 发布的时候,首先是需 ...

  3. mysql数据库主外键级联删除脚本RESTRICT --> CASCADE

    在项目中,我们一般在数据库设计的时候做主外键关联设计,要么就不做.但是这样不符合规范,呵呵. 建立主外键关系的时候,默认是不能级联删除的.而出现往往在删除主表的数据时报错, 需要先删除从表然后再删除主 ...

  4. PHP 打印函数之 print print_r

    print 说明 int print ( string $arg ) 输出 arg print 实际上不是一个函数(它是一个语言结构),因此你可以不必使用圆括号来括起它的参数列表 参数 arg:输入数 ...

  5. Spring @ Component 的作用

    1.@controller 控制器(注入服务) 2.@service 服务(注入dao) 3.@repository dao(实现dao访问) 4.@component (把普通pojo实例化到spr ...

  6. hdoj 1875 畅通工程再续

    题目传送门:http://acm.hdu.edu.cn/showproblem.php?pid=1875 //9403289 2013-10-24 17:00:49 Accepted 1875 62M ...

  7. Javascript中appendChilid()内涵

    <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <m ...

  8. java基础知识回顾之---java String final类普通方法的应用之字符串数组排序

    /* * 1,给定一个字符串数组.按照字典顺序进行从小到大的排序. * {"nba","abc","cba","zz", ...

  9. DJANGO中,用QJUERY的AJAX的json返回中文乱码的解决办法

    和网上其它用JAVA或是PHP的实现不太一样, DJANGO中的解决办法如下: 后端样例: def render_to_json_response(context, **response_kwargs ...

  10. Centos7安装Xmind

    1.首先,下载对应版本的deb包,32bit系统下载32bit软件包,64bit系统下载64bit软件包 2.解压deb包,得到data.tar.gz 和control.tar.gz 两个归档文件 3 ...