TabHost实现底部导航栏
源代码及可执行文件下载地址: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实现底部导航栏的更多相关文章
- Android应用底部导航栏(选项卡)实例
现在很多android的应用都采用底部导航栏的功能,这样可以使得用户在使用过程中随意切换不同的页面,现在我采用TabHost组件来自定义一个底部的导航栏的功能. 我们先看下该demo实例的框架图: 其 ...
- Android (争取做到)最全的底部导航栏实现方法
本文(争取做到)Android 最全的底部导航栏实现方法. 现在写了4个主要方法. 还有一些个人感觉不完全切题的方法也会简单介绍一下. 方法一. ViewPager + List<View> ...
- Android UI-仿微信底部导航栏布局
现在App基本的标配除了侧滑菜单,还有一个就是底部导航栏,常见的聊天工具QQ,微信,购物App都有底部导航栏,用户可以随便切换看不同的内容,说是情怀也好,用户体验也罢.我们开发的主要的还是讲的是如何如 ...
- 【转】Android应用底部导航栏(选项卡)实例
现在很多android的应用都采用底部导航栏的功能,这样可以使得用户在使用过程中随意切换不同的页面,现在我采用TabHost组件来自定义一个底部的导航栏的功能. 我们先看下该demo实例的框架图: 其 ...
- TextView+Fragment实现底部导航栏
前言:项目第二版刚上线没多久,产品又对需求进行了大改动,以前用的是左滑菜单,现在又要换成底部导航栏,于是今天又苦逼加班了.花了几个小时实现了一个底部导航栏的demo,然后总结一下.写一篇博客.供自己以 ...
- Android 修改底部导航栏navigationbar的颜色
Android 修改底部导航栏navigationbar的颜色 getWindow().setNavigationBarColor(Color.BLUE); //写法一 getWindow().set ...
- Android底部导航栏——FrameLayout + RadioGroup
原创文章,转载请注明出处http://www.cnblogs.com/baipengzhan/p/6285881.html Android底部导航栏有多种实现方式,本文详细介绍FrameLayout ...
- Android底部导航栏创建——ViewPager + RadioGroup
原创文章,引用请注明出处:http://www.cnblogs.com/baipengzhan/p/6270201.html Android底部导航栏有多种实现方式,本文详解其中的ViewPager ...
- 二、Fragment+RadioButton实现底部导航栏
在App中经常看到这样的tab底部导航栏 那么这种效果是如何实现,实现的方式有很多种,最常见的就是使用Fragment+RadioButton去实现.下面我们来写一个例子 首先我们先在activi ...
随机推荐
- SpringBoot系列: 理解 Spring 的依赖注入(二)
==============================Spring 容器中 Bean 的名称==============================声明 bean 有两个方式, 一个是 @B ...
- 调用wait的SIGCHLD信号处理函数
#include <stdio.h> #include <sys/wait.h> void sig_chld(int signo) { pid_t pid; int stat; ...
- 十、uboot 代码流程分析---run_main_loop
调用 board_init_r,传入全局 GD 和 SDRAM 中的目的地址 gd->rellocaddr void board_init_r(gd_t *new_gd, ulong dest_ ...
- 另一种的SQL注入和DNS结合的技巧
这个技巧有些另类,当时某业界大佬提点了一下.当时真的真的没有理解到那种程度,现在可能也是没有理解到,但是我会努力. 本文章是理解于:http://netsecurity.51cto.com/art/2 ...
- Rsync + inotify 实现文件实时同步
Rsync 用来实现触发式的文件同步. Inotify-tools是一套组件,Linux内核从2.6.13版本开始提供了inotify通知接口,用来监控文件系统的各种变化情况,如文件存取.删除.移动等 ...
- luogu P3172 [CQOI2015]选数
传送门 颓了一小时柿子orz 首先题目要求的是\[\sum_{x_1=l}^{r}\sum_{x_2=l}^{r}...\sum_{x_n=l}^{r}[gcd(x_1,x_2...x_n)=k]\] ...
- Linux/Windows双系统引导修复
安装双系统建议先安装windows,然后在安装Linux,使用Linux(grub2)引导双系统 如果重新安装了windows,则无法引导进入linux,需要修复引导 在windows下安装easyB ...
- 第20月第29天 cocoa抽象工厂 cocoapods组件化 cocoapods升级
1. 在 Cocoa Touch 框架中,类簇是抽象工厂模式在 iOS 下的一种实现,以 NSArray 举例,将原有的 alloc+init 拆开写: id obj1 = [NSArray allo ...
- Spring+thymeleaf
1.导入jar包 2.配置 3.标签使用 th:text th:utext th:object th:if th:switch th:case th:each="person:userlis ...
- GitHub贡献第一的公司是谁?微软开源软件列表
参考:http://www.infoq.com/cn/news/2017/03/GitHub-first-Microsoft-open-sour 提起微软公司,不少人第一反应是老牌巨头专注于私有化软件 ...