Android---61---TabHost简单使用

与TabHost结合使用的组件:

TabWidget:代表选项卡的标签条

TabSpec:代表选项卡的一个Tab页面

TabHost仅仅是一个简单的容器,它提供两个方法来创建、添加选项卡

newTabSpec(String tag):创建选项卡

addTab(TabHOst.TabSpec tabSpec):添加选项卡

步骤:

1.在界面布局文件中定义TabHost组件,并为该组件定义该选项卡的内容

2.Activity继承TabActivity

3.调用TAbActivity的getTabHost()方法获取TabHost对象

4.通过TabHost对象的方法来创建、添加选项卡

布局文件:

<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/tabhost"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_weight="1" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >

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

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

            <!-- 定义第一个标签页的内容 -->

            <LinearLayout
                android:id="@+id/tab01"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:orientation="vertical" >

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="tab1"
                    android:textSize="11pt" />
            </LinearLayout>
            <!-- 定义第二个标签页的内容 -->

            <LinearLayout
                android:id="@+id/tab02"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:orientation="vertical" >

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="tab2"
                    android:textSize="11pt" />
            </LinearLayout>
            <!-- 定义第三个标签页的内容 -->

            <LinearLayout
                android:id="@+id/tab03"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:orientation="vertical"
                android:textSize="11pt" >

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="tab3"
                    android:textSize="11pt" />
            </LinearLayout>
        </FrameLayout>
    </LinearLayout>

</TabHost>

TabHost容器内部需要组合两个组件:TabWidget和FrameLayout,其中TabWidget定义选项卡的标题条

Framelayout则用于层叠组合多个选项界面

Activity:

public class MainActivity extends TabActivity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		// 获取该Activity里面的TabHost组件
		TabHost tabHost = getTabHost();
		// 创建第一个Tab页
		TabSpec tab1 = tabHost.newTabSpec("tab1").setIndicator("01") // 设置标题
				.setContent(R.id.tab01); // 设置内容
		// 添加第一个标签页
		tabHost.addTab(tab1);
		TabSpec tab2 = tabHost
				.newTabSpec("tab2")
				// 在标签标题上放置图标
				.setIndicator("02",
						getResources().getDrawable(R.drawable.ic_launcher))
				.setContent(R.id.tab02);
		// 添加第二个标签页
		tabHost.addTab(tab2);
		TabSpec tab3 = tabHost.newTabSpec("tab3").setIndicator("03")
				.setContent(R.id.tab03);
		// 添加第三个标签页
		tabHost.addTab(tab3);
	}
}

有时候选项卡是在下边的,只需要修改布局文件即可

布局文件:

<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/tabhost"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_weight="1" >

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

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

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

            <!-- 定义第一个标签页的内容 -->

            <LinearLayout
                android:id="@+id/tab01"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:orientation="vertical" >

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="tab1"
                    android:textSize="11pt" />
            </LinearLayout>
            <!-- 定义第二个标签页的内容 -->

            <LinearLayout
                android:id="@+id/tab02"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:orientation="vertical" >

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="tab2"
                    android:textSize="11pt" />
            </LinearLayout>
            <!-- 定义第三个标签页的内容 -->

            <LinearLayout
                android:id="@+id/tab03"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:orientation="vertical"
                android:textSize="11pt" >

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="tab3"
                    android:textSize="11pt" />
            </LinearLayout>
        </FrameLayout>
    </RelativeLayout>

</TabHost>

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-12-10 19:55:34

Android---61---TabHost简单使用的相关文章

Android底部TabHost API

今天在项目中遇到了底部TabHost,顺便就写了一个底部TabHost的api继承即可使用非常简单,以下为源代码: 首先是自定义的TabHostActivity,如果要使用该TabHost继承该类即可 1 package com.api; 2 3 import android.app.TabActivity; 4 import android.content.Intent; 5 import android.os.Bundle; 6 import android.view.LayoutInfla

【原创】android——SQLite实现简单的注册登陆(已经美化)

1,Main_activity的xmL配置 1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 xmlns:tools="http://schemas.android.com/tools" 3 android:layout_width="match_parent" 4 android:layout_height="match_pa

TabHost 简单使用方法

package com.google.tabhost; import android.app.TabActivity; import android.os.Bundle; import android.view.LayoutInflater; import android.widget.TabHost; public class HelloTabHost extends TabActivity { /** Called when the activity is first created. */

Android的TabHost组件-android的学习之旅(四十)

TabHost简介 虽然,官方建议用Fagment取代TabHost,但是我们还是大概的介绍一下.TabHost是一种非常简单的组件,TabHost可以很方便的在窗口放置多个标签页,每一个标签页相当于获得了一个摆放位置. 注意 TabHost的内部需要两个组件一个是TabWidget和FrameLayout两个组件. 通话记录界面 <?xml version="1.0" encoding="utf-8"?> <TabHost xmlns:andro

12.Android之Tabhost组件学习

TabHost是整个Tab的容器,TabHost的实现有两种方式: 第一种继承TabActivity,从TabActivity中用getTabHost()方法获取TabHost.各个Tab中的内容在布局文件中定义就行了. 第二种方式,不继承TabActivity,在布局文件中定义TabHost即可,但是TabWidget的id必须是@android:id/tabs,FrameLayout的id必须是@android:id/tabcontent. 1)继承TabActivity 如果加载该TabH

Android:PopupWindow简单弹窗改进版

Android:PopupWindow简单弹窗 继续上一节的内容,改进一下,目标是点击菜单后把菜单收缩回去并且切换内容,我使用的是PopupWindow+RadioGroup public class MainActivity extends TabActivity { private PopupWindow pop; private TabHost tabhost; private RadioGroup radiogroup; private RadioButton tab1,tab2; @O

Android ViewPager+TabHost实现首页导航

今天发的是TabHost结合ViewPager实现首页底部导航的效果,虽然说网上有很多这样的Demo,不过呢,我还是要把自己练习写的发出来,没错!就是这么任性: 先上效果图,如下: 代码里面有注释,就不过多解释了,说几点需要注意的问题 1:TabHost .TabWidget.FrameLayout一定添加id这个属性,否则会报错 android:id="@android:id/tabhost" android:id="@android:id/tabcontent"

Android学习之简单的数据存储

在Android中,数据存储是开发人员不可以避免的.Android为开发者提供了很多的存储方法,在前面的博客中,已经讲述了sqlite存储数据.今天将介绍用SharedPreferences来存储数据,它可以将数据保存在应用软件的私有存储区,存储区的数据只能被写入这些数据的软件读取.SharedPreference通过键值对的方法存储数据. 1.SharedPreference存储简单数据 SharedPreference可以存放简单的String.Boolean.Int等对象. 1 <Rela

Android ExpandableListView的简单应用

Expandablelistview1Activity.java package com.wangzhu.demoexpandablelistview; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import android.app.Activity; import android.os.Bundle; import android.widg

Android HttpGet() 请求简单入门实例

HttpClient httpclient = new DefaultHttpClient(); String url = "http://example.com"; List<NameValuePair> params = new ArrayList<NameValuePair>(); params.add( new BasicNameValuePair( "param", "value" ) ); URI uri =