700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > android学习--TabHost选项卡组件

android学习--TabHost选项卡组件

时间:2023-09-26 17:36:03

相关推荐

android学习--TabHost选项卡组件

TabHost是一种非常有用的组件,TabHost能够非常方便地在窗体上放置多个标签页,每一个标签页获得了一个与外部容器同样大小的组件摆放区域。在手机系统的应用类似“未接电话”、“已接电话”、“呼出电话”等。

1 、 TabHost提供了两个方法来创建选项卡、加入选项卡

newTabSpec(String tag) : 创建选项卡

addTab(TabHost.TabSpec tabSpec) : 加入选项卡

2、TabHost 切换选项卡触发的监听是TabHost.OnTabChangeListener

以下通过案例来熟悉TabHost

使用TabHost的一般步骤为:

(1)在界面布局中为TabHost定义改选项卡的内容

(2)Activity继承TabActivity

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

(4)通过TabHost对象的方法来创建选项卡、加入选项卡

跟着上面步骤来实现TabHost案例

(1)在界面布局中为TabHost定义改选项卡的内容,一般採用FrameLayout作为根布局,每一个标签页面相应一个子节点的Layout

<!-- 这是根布局 --><FrameLayout xmlns:android="/apk/res/android"android:layout_width="fill_parent" android:layout_height="fill_parent"><!-- 这是第一个Tab布局 --><LinearLayout android:id="@+id/widget_layout_Blue"android:layout_width="fill_parent" android:layout_height="fill_parent"android:orientation="vertical" ><EditText android:id="@+id/widget34"android:layout_width="fill_parent"android:layout_height="wrap_content" android:text="EditText"android:textSize="18sp"></EditText><Button android:id="@+id/widget30"android:layout_width="wrap_content"android:layout_height="wrap_content" android:text="Button"></Button></LinearLayout><!-- 这是第二个Tab布局 --><LinearLayout android:id="@+id/widget_layout_red"android:layout_width="fill_parent"android:layout_height="fill_parent"android:orientation="vertical" ><AnalogClock android:id="@+id/widget36"android:layout_width="wrap_content"android:layout_height="wrap_content"></AnalogClock></LinearLayout><!-- 这是第三个Tab布局 --><LinearLayout android:id="@+id/widget_layout_green"android:layout_width="fill_parent" android:layout_height="fill_parent"android:orientation="vertical"><RadioGroupandroid:id="@+id/widget43"android:layout_width="166px"android:layout_height="98px"android:orientation="vertical"><RadioButton android:id="@+id/widget44"android:layout_width="wrap_content" android:layout_height="wrap_content"android:text="男"></RadioButton><RadioButton android:id="@+id/widget45"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="女"></RadioButton></RadioGroup></LinearLayout></FrameLayout>

(2)Activity继承TabActivity

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

(4)通过TabHost对象的方法来创建选项卡、加入选项卡

package com.example.tabhost;import android.app.TabActivity;import android.os.Bundle;import android.util.Log;import android.view.LayoutInflater;import android.widget.TabHost;import android.widget.TabHost.OnTabChangeListener;@SuppressWarnings("deprecation")public class MainActivity extends TabActivity{@Overridepublic void onCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);//获取TabHost 对象TabHost tabHost = getTabHost();//设置使用TabHost布局//from(this)从这个TabActivity获取LayoutInflater//R.layout.main 存放Tab布局//通过TabHost获得存放Tab标签页内容的FrameLayout//是否将inflate 拴系到根布局元素上LayoutInflater.from(this).inflate(R.layout.activity_main,tabHost.getTabContentView(), true);//加入第一个标签页//setIndicator 加入表体,能够是view//加入tab内容 布局tabHost.addTab(tabHost.newTabSpec("tab1").setIndicator("TAB1").setContent(R.id.widget_layout_Blue)); //加入第二个标签页tabHost.addTab(tabHost.newTabSpec("tab2")//在标签标题上放置图标.setIndicator("TAB2").setContent(R.id.widget_layout_red)); //加入第三个标签页tabHost.addTab(tabHost.newTabSpec("tab3").setIndicator("TAB3").setContent(R.id.widget_layout_green)); //加入监听tabHost.setOnTabChangedListener(new OnTabChangeListener() {@Overridepublic void onTabChanged(String tabId) {Log.i("Tab", tabId);}});}}

执行后,效果例如以下:

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。