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);
    }
}
时间: 2024-07-28 15:04:00

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

【Android基础篇】TabHost实现底部导航栏

在App应用中,导航栏往往是用于解决功能分块的最佳控件,而底部导航栏更是导航栏中最常用的,因为它位于屏幕底部,用户操作起来会很方便. 下面介绍一下使用Android控件TabHost实现底部导航栏的方法. TabHost可以在控件库里直接拖到页面上,非常方便,但拖出来的是顶部导航栏,如下图所示: 到这里就可以开始实现底部导航栏了,我们首先转到它的XML布局代码里,然后修改成下面这样: <FrameLayout xmlns:android="http://schemas.android.co

实现底部导航栏

许多的App都使用底部导航栏来实现导航功能,我们可以使用RadioGroup+RadioButton的形式或者直接Button数组的方式实现,而谷歌官方提供了FragmentTabHost来方便快捷实现底部导航栏. android.support.v4.app.FragmentTabHost 主要代码: fragmentTabHost.setup(this, getSupportFragmentManager(), R.id.layout_content); for (int i = 0; i 

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

现在很多android的应用都采用底部导航栏的功能,这样可以使得用户在使用过程中随意切换不同的页面,现在我采用TabHost组件来自定义一个底部的导航栏的功能. 我们先看下该demo实例的框架图: 其中各个类的作用以及资源文件就不详细解释了,还有资源图片(在该Demo中借用了其它应用程序的资源图片)也不提供了,大家可以自行更换自己需要的资源图片.直接上各个布局文件或各个类的代码: [1]  res/layout目录下的 maintabs.xml 源码: <?xml version="1.0

FragmentTabHost+FrameLayout实现底部导航栏

app经常用到底部导航栏,早前使用过RadioGroup+FrameLayout实现或者RadioGroup+ViewPager实现,现在基本使用FragmentTabHost+FrameLayout来实现,因为使用起来简单易用.下面写一个小例子简要地总结一下这个组合. 首先,看一下例子的最终运行效果图 -> 这5个图标的效果其实都是一样的,只要做出来一个,以此类推就可以写出其他几个 第一步, FragmentTabHost+FrameLayout布局,先看一下代码: <?xml versio

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

现在App基本的标配除了侧滑菜单,还有一个就是底部导航栏,常见的聊天工具QQ,微信,购物App都有底部导航栏,用户可以随便切换看不同的内容,说是情怀也好,用户体验也罢.我们开发的主要的还是讲的是如何如何实现其功能,网上实现的方式无外乎两种,一种是使用tabhost进行切换,一种是直接使用Fragment进行切换,底部导航栏的布局有的使用的是线性布局,有的是使用的RadioGroup,本文中是使用fragment+RadioGroup是实现的,看正文吧: 基础布局 其中主要低 底部导航栏,其他都没

关于在Android中如何设置底部导航栏

Android应用底部导航栏(选项卡)实例 现在很多android的应用都采用底部导航栏的功能,这样可以使得用户在使用过程中随意切换不同的页面,现在我采用TabHost组件来自定义一个底部的导航栏的功能. 我们先看下该demo实例的框架图:   其中各个类的作用以及资源文件就不详细解释了,还有资源图片(在该Demo中借用了其它应用程序的资源图片)也不提供了,大家可以自行更换自己需要的资源图片.直接上各个布局文件或各个类的代码: [1]  res/layout目录下的maintabs.xml 源码

使用FragmentTabHost 完成一个简单的底部导航栏

使用FragmentTabhost+Fragment实现一个底部导航栏 主布局: //放置Fragment <FrameLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" android:id="@+id/fragment"> </FrameLayou

Android实习札记(5)---Fragment之底部导航栏的实现

Android实习札记(5)---Fragment之底部导航栏的实现 --转载请注明出处:coder-pig 在Part 4我们回顾了一下Fragment的基本概念,在本节中我们就来学习Fragment应用的简单例子吧! 就是使用Fragment来实现简单的底部导航栏,先贴下效果图: 看上去很简单,实现起来也是很简单的哈!那么接着下来就看下实现的流程图吧: 实现流程图: 看完流程图是不是有大概的思路了,那么接着就开始代码的编写吧: 代码实现: ①先写布局,布局的话很简单,一个FrameLayou

移动端底部导航栏固定——兼容IOS

问题:移动端前端底部导航栏设计 兼容安卓下的各种浏览器和IOS自带的Safari,但是不兼容苹果下的 钉钉. 具体代码格式: <body> <!-- 头部导航栏 --> <div class="header">内容</div> <!-- 内容 --> <div class="content">内容</div> <!-- 底部导航栏 --> <div class=&q