TabHost实现底部导航栏

时间:2022-09-24 09:16:51

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

TabHost实现底部导航栏   TabHost实现底部导航栏

TabHost实现底部导航栏  TabHost实现底部导航栏  TabHost实现底部导航栏

现在很多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应用底部导航栏&lpar;选项卡&rpar;实例

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

  2. Android &lpar;争取做到&rpar;最全的底部导航栏实现方法

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

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

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

  4. 【转】Android应用底部导航栏&lpar;选项卡&rpar;实例

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

  5. TextView&plus;Fragment实现底部导航栏

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

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

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

  7. Android底部导航栏——FrameLayout &plus; RadioGroup

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

  8. Android底部导航栏创建——ViewPager &plus; RadioGroup

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

  9. 二、Fragment&plus;RadioButton实现底部导航栏

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

随机推荐

  1. Python 文件操作模块 shutil 详解

    1.导入模块 shutil import shutil 2.shutil方法 2.1 shutil.copy(src,dst) //将 src 复制到 dst 保留文件权限    例:将Alan复制到 ...

  2. apktool给软件加注册机修改图标和文件名

    功能实现,即让你的软件具有注册机功能,或者破解别人的软件,据为己有! 先反编译文件包 然后全局工具,修改图标和名称 加注册机,输入key,下载计算器,即可.给某个用户设置自定义的使用时间!

  3. 3&period;django学习

    ##另外一种url配置方法 首先要导入include 要包含blog目录下的urls.py(新建)的文件 从views连接到index

  4. 如何将在线电子书保存为pdf格式

    网上有很多免费的在线电子书籍,没有pdf格式,不方便离线阅读,也不方便做记录,所以找了几个将在线内容制作成pdf文件的方法. 一.如果网站上的书籍内容没有分页,所有内容都直接显示出来了,最简单,直接将 ...

  5. POJ-2253 Frogger(最短路)

    https://vjudge.net/problem/POJ-2253 题意 公青蛙想到母青蛙那里去,期间有许多石头,公青蛙可以通过这些石头跳过去.问至少要跳的最大距离,即所有路径上石头间的最大距离的 ...

  6. centos 安装cloud foundry CLI

    步骤: 1.wget -O /etc/yum.repos.d/cloudfoundry-cli.repo https://packages.cloudfoundry.org/fedora/cloudf ...

  7. python16&lowbar;day14【jQuery】

    一.作用域 1.作用域例一 <script> var str = "global"; //AO1 AO1.str function t(age){ console.lo ...

  8. &lbrack;javascript&rsqb;什么是闭包?

    http://www.zcfy.cc/article/master-the-javascript-interview-what-is-a-closure-2127.html

  9. Android面试收集录12 View测量、布局及绘制原理

    一.View绘制的流程框架 View的绘制是从上往下一层层迭代下来的.DecorView-->ViewGroup(--->ViewGroup)-->View ,按照这个流程从上往下, ...

  10. 企业版 Linux 附加软件包&lpar;EPEL&rpar;

    企业版 Linux 附加软件包(以下简称 EPEL)是一个由特别兴趣小组创建.维护并管理的,针对 红帽企业版 Linux(RHEL)及其衍生发行版(比如 CentOS.Scientific Linux ...