TabHost用法

tabhost用两种方法

方法一:Activity继承TabActivity后用getTabHost()方法来获取tabhost(前提:Activity的setContentView要删除,这样布局才能用TabActivity的默认布局,这样TabActivity的setup方法才不会出错),获取完后再对tabhost布局进行设置

方法二:用findViewById方法来获取tabhost,只是findViewById里的布局xml要符合一以下的格式

<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/tabhost"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <LinearLayout
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">
        <TabWidget
            android:id="@android:id/tabs"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" />
        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent">  

        </FrameLayout>
    </LinearLayout>
</TabHost>  

然后调用tabhost.setup()方法,再调用addTab()方法

代码如下:

MyTabActivity

package com.my.studyofandroid;

import android.os.Bundle;
import android.app.Activity;
import android.text.AndroidCharacter;
import android.view.LayoutInflater;
import android.view.Menu;
import android.widget.TabHost;
///tabhost方法一:继承TabActivity类,用getTabHost()方法来建tabhost。这时setContentView()要去掉,不限制xml布局文件里是否要有Tabhost,TabWidget,FrameLayout等 内容并且格式要固定
@SuppressWarnings("deprecation")
public class MyTabActivity extends android.app.TabActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        ///tabhost方法一
        super.onCreate(savedInstanceState);
    //    setContentView(R.layout.activity_mytab);///这句话要删除不然会报错:Your TabHost must have a TabWidget whose id attribute is ‘android.R.id.tabs
        ///用TabActivity的方法获取tabhost对象
        TabHost tabhost=getTabHost();
        //设置使用tabhost的布局
        LayoutInflater.from(this).inflate(R.layout.activity_mytab, tabhost.getTabContentView(),true);
        ///加tab,tab里的布局是由另一个xml决定
        tabhost.addTab(tabhost.newTabSpec("tab1").setContent(R.id.tab_1).setIndicator("选择卡1"));
        tabhost.addTab(tabhost.newTabSpec("tab2").setContent(R.id.tab_2).setIndicator("选择卡2"));

        /*
        ///tabhost方法二:如果用方法二,可以不继承TabActivity了,因为用不到getTabHost方法
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_mytab);///activity_mytab.xml里的格式见最下面代码

        TabHost tabHost=(TabHost)findViewById(R.id.tabhost);///获取TabHost 通过findviewbyid
        tabhost.setup();///在加tab到tabhost之前要执行此方法,也就是说通过findviewbyid方法获得tabhost必须setup(), 而通过getTabHost则不用。查看setup源码就知道为什么TabWidget和FrameLayout必须用系统自定义的id
        ///加tab
        tabhost.addTab(tabhost.newTabSpec("tab1").setContent(R.id.tab_1).setIndicator("选择卡1"));
        tabhost.addTab(tabhost.newTabSpec("tab2").setContent(R.id.tab_2).setIndicator("选择卡2"));
        */
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.tab, menu);
        return true;
    }

}

///如果用方法二,activity_mytab。xml的格式必须如下:当然要包含Tabhost,TabWidget,FrameLayout,着3个标签:
/*
<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/tabhost"  ///id必须为tabhost
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <LinearLayout
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">
        <TabWidget
            android:id="@android:id/tabs"  ///id必须为tabs
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" />
        <FrameLayout
            android:id="@android:id/tabcontent"  ///id必须为tabcontent
            android:layout_width="fill_parent"
            android:layout_height="fill_parent">
        ///下面是两个tab布局   

    <LinearLayout
        android:id="@+id/tab_1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView1"
        android:layout_marginTop="72dp"
        android:layout_toRightOf="@+id/textView1"
        android:orientation="vertical" >

        <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button" />

        <Button
            android:id="@+id/button2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button" />

    </LinearLayout>

    <LinearLayout
        android:id="@+id/tab_2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/tab_1"
        android:layout_below="@+id/tab_1"
        android:layout_marginTop="116dp"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/textView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="TextView" />

        <TextView
            android:id="@+id/textView3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="TextView" />

    </LinearLayout>

        </FrameLayout>
    </LinearLayout>
</TabHost> 

*/

activity_mytab.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MyTabActivity" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />

    <LinearLayout
        android:id="@+id/tab_1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView1"
        android:layout_marginTop="72dp"
        android:layout_toRightOf="@+id/textView1"
        android:orientation="vertical" >

        <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button" />

        <Button
            android:id="@+id/button2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button" />

    </LinearLayout>

    <LinearLayout
        android:id="@+id/tab_2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/tab_1"
        android:layout_below="@+id/tab_1"
        android:layout_marginTop="116dp"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/textView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="TextView" />

        <TextView
            android:id="@+id/textView3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="TextView" />

    </LinearLayout>

</RelativeLayout>

时间: 2024-08-01 15:11:09

TabHost用法的相关文章

tabhost使用

Tabhost用法 使用方法一:使用同一个布局文件 在xml中如此定义tabhost: <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_heigh

Android TabHost(简易用法)

前言 欢迎大家我分享和推荐好用的代码段~~ 声明          欢迎转载,但请保留文章原始出处: CSDN:http://www.csdn.net 雨季o莫忧离:http://blog.csdn.net/luckkof 正文 Tab应用的结构 TabHost的Activity的结构如下: <?xml version="1.0" encoding="utf-8"?> <!-- 定义TabHost组件 --> <LinearLayout

TabHost的用法(转)

本文结合源代码和实例来说明TabHost的用法.      使用TabHost 可以在一个屏幕间进行不同版面的切换,例如android自带的拨号应用,截图:       查看tabhost的源代码,主要实例变量有: private TabWidget mTabWidget;    private FrameLayout mTabContent;    private List<TabSpec> mTabSpecs 也就是说我们的tabhost必须有这三个东西,所以我们的.xml文件就会有规定:

TabHost的用法

今天学习了TabHost组件的功能和用法,在这里将Demos展现一下. activity_tab_host_demos.xml <TabHost xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android

Tabhost以及其用法

TabHost是一种非常使用的组件,TabHost可以方便的在窗口上放置多个标签页,每个标签页相当于或得了一个与外部容器相同大小的组件摆放区域. 与TabHost 结合使用的组件 TabWidget:代表选项卡的标签条. TabSpec:代表选项卡的一个Tab页面. 创建添加选项卡的方法: newTabSpec():创建选项卡 addTab():添加选项卡 使用TabHost的步骤: 在界面布局中定义TabHost组件 Activity继承TabActivity 调用TabActivity的ge

Fragment + TabHost + RadioGroup

1. 使用FragMent是因为 4.0.3之后 ,摒弃了TabActivity这种用法, Demo 效果图: 先上布局XML R.layout.activity_main [html] view plaincopy <?xml version="1.0" encoding="utf-8"?> <TabHost xmlns:android="http://schemas.android.com/apk/res/android"

Android零基础入门第70节:ViewPager轻松完成TabHost效果

上一期学习了ViewPager的简单使用,本期一起来学习ViewPager的更多用法. 相信很多同学都使用过今日头条APP吧,一打开主界面就可以看到顶部有很多Tab,然后通过左右滑动来切换,就可以通过ViewPager来完成.当然具体实现又会有很多方式,我们本期就先来学习最简单的Tab切换吧,有一点类似于之前了解的TabHost. 一.PagerTitleStrip与PagerTabStrip 在实际运用中,很多时候只有页面滑动是不够的,还需要有标题栏才够友好.首先来学习一下官方自带的,在and

实现360手机助手TabHost的波纹效果

现在新版360手机助手的界面都做得挺漂亮的,在切换底部导航时的波纹效果也很好看,刚好最近看了个开源项目才了解到原来Drawable做动画效果也怎么好用,所以就仿照360实现了下带波纹的TabHost.源代码地址:https://github.com/Rukey7/XFragmentTabHost 先来看一下实现后的效果: 说明一下实现要点: 1. 因为我们项目之前用的是FragmentTabHost,所以我直接继承FragmentTabHost来实现动画效果更方便: 2. 波纹动画的实现其实是自

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

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