源代码及可执行文件下载地址:http://files.cnblogs.com/rainboy2010/tabnavigation.zip

   

    

现在很多Android应用界面都采用底部导航栏的设计方式,这样可以使用户灵活的切换不同的页面。采用TabHost控件很容易实现一个底部导航栏的功能,下面以模仿鲁大师客户端底部导航栏为例小试牛刀

1.设计主界面,布局文件tab_ludashi.xml如下:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/container"
android:layout_width="fill_parent"
android:layout_height="fill_parent" > <TabHost
android:id="@android:id/tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/bg"
> <LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" > <FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="0.0dp"
android:layout_weight="1"
>
</FrameLayout>
<TabWidget
android:id="@android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="@dimen/tabwidget_height"
android:gravity="center"
android:showDividers="none"
>
</TabWidget> </LinearLayout> </TabHost> </FrameLayout>

每一个TabItem对应的布局文件tab_ludashi_item.xml如下,图片在上部,文字在下部

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/tabwidget_item_layout"
android:layout_width="fill_parent"
android:layout_height="@dimen/tabwidget_height"
android:orientation="vertical"
android:gravity="center"
android:background="@drawable/selector_ludashi_tabitem_bg"
>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<ImageView
android:id="@+id/tabwidget_item_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:contentDescription="@null"
android:scaleType="fitCenter"
/>
<ImageView
android:id="@+id/tabwidget_item_dot"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toRightOf="@id/tabwidget_item_image"
android:contentDescription="@null"
android:scaleType="fitCenter"
android:visibility="invisible"
android:src="@drawable/red_dot"
/>
</RelativeLayout> <TextView
android:id="@+id/tabwidget_item_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:layout_marginTop="3dp"
android:textSize="12sp"
android:textColor="@drawable/selector_ludashi_tabitem_text"
/> </LinearLayout>

选中状态和未选中状态下背景对应的xml文件selector_ludashi_tabitem_bg.xml为:

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

选中状态和未选中状态下文字颜色对应的xml文件selector_ludashi_tabitem_text.xml:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:color="#ff00a5df" />
<item android:state_selected="true" android:color="#ff00a5df" />
<item android:color="#ff797979" />
</selector>

2.设计每一个TabItem选中状态和未选中状态对应的图片,以第一个Item为例,对应的xml文件selector_ludashi_tabitem_image_myphone.xml如下:

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

3.编写Java代码,如下:

@SuppressWarnings("deprecation")
public class LudashiActivity extends TabActivity
{ private TabHost mTabHost;
private int []mTabImage=new int[]{R.drawable.selector_ludashi_tabitem_image_myphone,R.drawable.selector_ludashi_tabitem_image_bench,
R.drawable.selector_ludashi_tabitem_image_optimize,R.drawable.selector_ludashi_tabitem_image_find};
private int []mTabText=new int[]{R.string.ludashi_tab1,R.string.ludashi_tab2,R.string.ludashi_tab3,R.string.ludashi_tab4};
private String[]mTabTag=new String[]{"tab1","tab2","tab3","tab4"};
private Class<?>[] mTabClass=new Class<?>[]{Tab1.class,Tab2.class,Tab3.class,Tab4.class}; @Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.tab_ludashi); initUI(); } private void initUI()
{
this.setTitle(R.string.button2); this.mTabHost=this.getTabHost();
this.mTabHost.setup(); //设置显示的图片和文字
for(int i=0;i<mTabClass.length;i++)
{
View view=LayoutInflater.from(this).inflate(R.layout.tab_ludashi_item, null); ((ImageView)view.findViewById(R.id.tabwidget_item_image)).setImageResource(mTabImage[i]);
((TextView)view.findViewById(R.id.tabwidget_item_text)).setText(mTabText[i]); this.mTabHost.addTab(this.mTabHost.newTabSpec(mTabTag[i]).setIndicator(view).setContent(new Intent(this,mTabClass[i])));
} //设置默认选中项
this.mTabHost.setCurrentTab(0);
}
}

TabHost实现底部导航栏的更多相关文章

  1. Android应用底部导航栏(选项卡)实例

    现在很多android的应用都采用底部导航栏的功能,这样可以使得用户在使用过程中随意切换不同的页面,现在我采用TabHost组件来自定义一个底部的导航栏的功能. 我们先看下该demo实例的框架图: 其 ...

  2. Android (争取做到)最全的底部导航栏实现方法

    本文(争取做到)Android 最全的底部导航栏实现方法. 现在写了4个主要方法. 还有一些个人感觉不完全切题的方法也会简单介绍一下. 方法一. ViewPager + List<View> ...

  3. Android UI-仿微信底部导航栏布局

    现在App基本的标配除了侧滑菜单,还有一个就是底部导航栏,常见的聊天工具QQ,微信,购物App都有底部导航栏,用户可以随便切换看不同的内容,说是情怀也好,用户体验也罢.我们开发的主要的还是讲的是如何如 ...

  4. 【转】Android应用底部导航栏(选项卡)实例

    现在很多android的应用都采用底部导航栏的功能,这样可以使得用户在使用过程中随意切换不同的页面,现在我采用TabHost组件来自定义一个底部的导航栏的功能. 我们先看下该demo实例的框架图: 其 ...

  5. TextView+Fragment实现底部导航栏

    前言:项目第二版刚上线没多久,产品又对需求进行了大改动,以前用的是左滑菜单,现在又要换成底部导航栏,于是今天又苦逼加班了.花了几个小时实现了一个底部导航栏的demo,然后总结一下.写一篇博客.供自己以 ...

  6. Android 修改底部导航栏navigationbar的颜色

    Android 修改底部导航栏navigationbar的颜色 getWindow().setNavigationBarColor(Color.BLUE); //写法一 getWindow().set ...

  7. Android底部导航栏——FrameLayout + RadioGroup

    原创文章,转载请注明出处http://www.cnblogs.com/baipengzhan/p/6285881.html Android底部导航栏有多种实现方式,本文详细介绍FrameLayout ...

  8. Android底部导航栏创建——ViewPager + RadioGroup

    原创文章,引用请注明出处:http://www.cnblogs.com/baipengzhan/p/6270201.html Android底部导航栏有多种实现方式,本文详解其中的ViewPager ...

  9. 二、Fragment+RadioButton实现底部导航栏

    在App中经常看到这样的tab底部导航栏   那么这种效果是如何实现,实现的方式有很多种,最常见的就是使用Fragment+RadioButton去实现.下面我们来写一个例子 首先我们先在activi ...

随机推荐

  1. MyBatis简单使用和入门理解

    本文记录第一次使用Mybatis时碰到的一些错误和简单理解,采用的示例是Eclipse中的JAVA工程,采用XML文件定义数据库连接. 可以使用Java JDBC API直接操作数据库,但使用框架会更 ...

  2. C++ vector和list的主要用法区别

    vector可以直接通过下标访问,list不可以 1.vector ; i < userTypes.size(); i++) { str = userTypes[i].typeName; } 2 ...

  3. Java8新特性_stream API 练习

    交易员类 public class Trader { private String name; private String city; public Trader() { } public Trad ...

  4. JQuery Rest客户端库

    JQuery Rest https://github.com/jpillora/jquery.rest/ Summary A jQuery plugin for easy consumption of ...

  5. 二十七、Linux 进程与信号---进程组和组长进程

    27.1 进程组 27.1.1 进程组介绍 进程组为一个或多个进程的集合 进程组可以接受同一终端的各种信号,同一个信号发送进程组等于发送给组中的所有进程 每个进程组有唯一的进程组 ID 进程组的消亡要 ...

  6. 六、文件IO——fcntl 函数 和 ioctl 函数

    6.1 fcntl 函数 6.1.1 函数介绍 #include <unistd.h> #include <fcntl.h> int fcntl(int fd, int cmd ...

  7. luogu 1850 换教室 概率+dp

    非常好的dp,继续加油练习dp啊 #include<bits/stdc++.h> #define rep(i,x,y) for(register int i=x;i<=y;i++) ...

  8. Asp.Net 初级 高级 学习笔记

    01.Main函数是什么?在程序中使用Main函数有什么需要注意的地方?02.CLR是什么?程序集是什么?当运行一个程序集的时候,CLR做了什么事情?03.值类型的默认值是什么?(情况一:字段或全局静 ...

  9. oracle 重建索引以及导出所有的索引脚本(可以解决还原数据库文件时先还原数据,在重新用脚本创建索引)

    导出数据库备份文件 1. 备份服务器数据,采用并行方式,加快备份速度(文件日期根据具体操作日期修改) expdp jhpt/XXXX directory=databackup dumpfile=dpf ...

  10. kvm 搭建

    一,准备环境   物理机 虚拟机 操作系统 CentOS 6.8 x64 CentOS 6.8 x64 CPU/内存 10核超线程x2/64G 2核/4G 外网IP -- 内网IP eth1_192. ...