Android Tab -- 使用TabWidget、TabHost、TabActivity来实现

原文地址http://blog.csdn.net/crazy1235/article/details/42678877

TabActivity在API13之后被fragment替代了,所以不建议使用

效果:点击头像标签,进行切换。

代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TabHost
        android:id="@android:id/tabhost"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <TabWidget
                android:id="@android:id/tabs"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_alignParentBottom="false"/>

            <FrameLayout
                android:id="@android:id/tabcontent"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_below="@android:id/tabs">

                <LinearLayout
                    android:id="@+id/tab1"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:background="#DEB887"
                    android:orientation="vertical">

                </LinearLayout>

                <LinearLayout
                    android:id="@+id/tab2"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:background="#BCEE68"
                    android:orientation="vertical">

                </LinearLayout>

                <LinearLayout
                    android:id="@+id/tab3"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:background="#7D9EC0"
                    android:orientation="vertical">

                </LinearLayout>
            </FrameLayout>

        </RelativeLayout>

    </TabHost>

</LinearLayout>

tabhost_tabwidget_tabactivity.xml

public class TabHostTabWidgetTabActivity extends TabActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.tabhost_tabwidget_tabactivity);

        TabHost tabHost = getTabHost(); //(TabHost) findViewById(android.R.id.tabhost);

        tabHost.addTab(tabHost
                .newTabSpec("111")
                .setIndicator("", getResources().getDrawable(R.drawable.wuyong))
                .setContent(R.id.tab1));

        tabHost.addTab(tabHost
                .newTabSpec("222")
                .setIndicator("", getResources().getDrawable(R.drawable.gongsunsheng))
                .setContent(R.id.tab2));

        tabHost.addTab(tabHost
                .newTabSpec("333")
                .setIndicator("", getResources().getDrawable(R.drawable.likui))
                .setContent(R.id.tab3));

        tabHost.setBackgroundColor(Color.argb(150, 22, 70, 150));

        tabHost.setCurrentTab(0);

        tabHost.setOnTabChangedListener(new TabHost.OnTabChangeListener() {
            @Override
            public void onTabChanged(String tabId) {
                Toast.makeText(TabHostTabWidgetTabActivity.this, tabId, Toast.LENGTH_SHORT).show();
            }
        });
    }
}

TabHostTabWidgetTabActivity.java

时间: 2024-08-08 18:38:13

Android Tab -- 使用TabWidget、TabHost、TabActivity来实现的相关文章

Android 实现分页(使用TabWidget/TabHost)

注:本文为转载,但该内容本人已亲身尝试,确认该方法可行,代码有点小的改动,转载用作保存与分享. 原作者地址:http://gundumw100.iteye.com/blog/853967 个人吐嘈:据说TabWidget已经过时了,取而代之的是Fragment,但关于这个Fragment暂时还没时间研讨,现使用的是TabWidget方法,个人感觉实现出来的效果还是很不错的. 自定义TabHost:不用继承TabActivity,具体代码如下: 定义布局文件:activity_main.xml <

Android Tab与TabHost

这就是Tab,而盛放Tab的容器就是TabHost 如何实现?? 每一个Tab还对应了一个布局,这个就有点好玩了.一个Activity,对应了多个功能布局. ①新建一个Tab项目,注意,不要生成main Activity 这里不要选 ②在包里面新建一个类MyTab,继承于TabActivity 其实,TabActivity是Activity的子类 package zyf.tab.test; import android.app.TabActivity; public class MyTab ex

Android开发之自定义TabHost文字及背景(源代码分享)

使用TabHost 可以在一个屏幕间进行不同版面的切换,而系统自带的tabhost界面较为朴素,我们应该如何进行自定义修改优化呢 MainActivity的源代码 package com.dream.ledong; import android.app.TabActivity; import android.content.Intent; import android.graphics.Color; import android.os.Bundle; import android.view.Gr

Android 自学之选项卡TabHost

选项卡(TabHost)是一种非常实用的组件,TabHost可以很方便地在窗口上放置多个标签页,每个标签页相当于获得了一个与外部容器相同大小的组建摆放区域.通过这种方式,就可以在一个容器中放置更多组件,例如许多手机系统会在同一个窗口第一多个标签页:来显示通话记录,包括“未接来电”,“已接来电”,“呼出电话”等. TabHost仅仅是一个简单的容器,他提供了如下两个方法来创建选项卡.添加选项卡. newTabSpec(String tag) : 创建选项卡 addTab(TabHost.TabSp

android之实现底部TabHost

转:http://www.open-open.com/lib/view/open1330697955842.html 先说布局文件,如下:利用android:layout_alignParentBottom="true" 实现底部显示 <?xml version="1.0" encoding="utf-8"?> <TabHost xmlns:android="http://schemas.android.com/apk

android学习笔记十——TabHost

TabHost——标签页 ==> TabHost,可以在窗口放置多个标签页,每个标签页相当于获得了一个与外部容器相同大小的组件摆放区域. 通过此种方式可以实现在一个容器放置更多组件(EG:通话记录实现方式). TabHost仅仅是一个简单的容器,其提供了如下两个方法来创建选项卡.添加选项卡 ==> newTabSpec(String tag):创建选项卡 addTab(TabHost.TabSpec tabspec):添加选项卡 使用TabHost的一般步骤: 1.在界面布局中定义TabHos

Android ActivityGroup实现的TabHost 保留activity原来状态

在用ActivityGroup实现的TabHost时,由tab1切换到tab2再切换回来时,会重新加载tab1里的activity public class MainUIActivity extends ActivityGroup{ private Context context=this; private TabHost mTabhost; private String TAB1="首页"; private String TAB2="收藏"; private St

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

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

Android乐学成语之TabHost介绍及使用Fragment 替换 TabActivity

TabHost介绍 TabHost组件可以在界面中存放多个选项卡, 很多软件都使用了改组件进行设计; 1. TabHost常用组件 TabWidget : 该组件就是TabHost标签页中上部 或者 下部的按钮, 可以点击按钮切换选项卡; TabSpec : 代表了选项卡界面, 添加一个TabSpec即可添加到TabHost中; -- 创建选项卡 : newTabSpec(String tag), 创建一个选项卡; -- 添加选项卡 : addTab(tabSpec); TabHost的使用请看