需求:使用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. docker设置代理

    在天朝使用docker需要FQ. 下面给出docker的代理方式: HTTP_PROXY=http://10.167.251.83:8080 docker -d

  2. 终于明白公测的beta 源自何处了

    A very early version of a software product that may not contain all of the features that are planned ...

  3. Sudoku Solver

    Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by th ...

  4. java基础知识回顾之java Thread类学习(八)--java多线程通信等待唤醒机制经典应用(生产者消费者)

     *java多线程--等待唤醒机制:经典的体现"生产者和消费者模型 *对于此模型,应该明确以下几点: *1.生产者仅仅在仓库未满的时候生产,仓库满了则停止生产. *2.消费者仅仅在有产品的时 ...

  5. Project Euler 95:Amicable chains 亲和数链

    Amicable chains The proper divisors of a number are all the divisors excluding the number itself. Fo ...

  6. lintcode 中等题:Simplify Path 简化路径

    题目 简化路径 给定一个文档(Unix-style)的完全路径,请进行路径简化. 样例 "/home/", => "/home" "/a/./b ...

  7. 【Linux高频命令专题(13)】cat

    概述 常用来显示文件内容,或者将几个文件连接起来显示,或者从标准输入读取内容并显示,它常与重定向符号配合使用. cat主要有三大功能: 1.一次显示整个文件:cat filename 2.从键盘创建一 ...

  8. Android:让EditText不自动获取焦点

    解决方法: 在EditText的父级控件中加入属性: android:focusable="true" android:focusableInTouchMode="tru ...

  9. eclipse 中忽略jsp, xml文件中的报错信息

    有的时候, 在eclipse中, jsp, xml 文件时运行的好好的, 可是就是在eclipse中报错, 虽然不影响功能, 但看起来很烦, 去掉这些错误警告的方法是: Windows-Prefere ...

  10. 87. Scramble String

    题目: Given a string s1, we may represent it as a binary tree by partitioning it to two non-empty subs ...