TabHost的使用

来源:互联网 发布:mac 终端设置时间 编辑:程序博客网 时间:2024/06/05 07:28

 TabActivity是一个过时的类,推荐使用Fragment.

实现选项卡的一般步骤:

1.在布局文件中添加实现选项卡所需要的TabHost,TabWidget,和FrameLayout组件。

2.编写各标签页中要显示内容所对应的xml布局文件。

3.在activity中,获取并初始化TabHost组件。

4.为TabHost对象添加标签页


xml文件:

<?xml version="1.0" encoding="utf-8"?><TabHost xmlns:android="http://schemas.android.com/apk/res/android"    android:id="@+id/c92_tabhost"    android:layout_width="fill_parent"    android:layout_height="fill_parent" >    <LinearLayout        android:layout_width="fill_parent"        android:layout_height="fill_parent"        android:orientation="vertical" >        <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" >            <AnalogClock                android:id="@+id/c92_tab1"                android:layout_width="fill_parent"                android:layout_height="fill_parent"                android:layout_centerHorizontal="true" />            <RelativeLayout                android:id="@+id/rll"                android:layout_width="match_parent"                android:layout_height="match_parent" >                <Button                    android:id="@+id/c92_tab2"                    android:layout_width="wrap_content"                    android:layout_height="wrap_content"                    android:onClick="onClick"                    android:text="A semi-random Button" />                <Button                    android:id="@+id/c92_tab3"                    android:layout_width="wrap_content"                    android:layout_height="wrap_content"                    android:layout_below="@id/c92_tab2"                    android:text="A semi-random Button" />            </RelativeLayout>        </FrameLayout>    </LinearLayout></TabHost>
在xml文件中添加选项卡组件时,必须使用系统的id,否则会报错。


activity中:

public class MainActivity extends Activity {private TabHost tabs;protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.tabhost);// 步骤1:获得TabHost的对象,并进行初始化setup()tabs = (TabHost) findViewById(R.id.c92_tabhost);tabs.setup();// 步骤2:通过TabHost.TabSpec增加tab的一页,通过setContent()增加内容,通过setIndicator增加页的标签/* (1)增加第1页 */TabHost.TabSpec spec; spec = tabs.newTabSpec("Tag1");spec.setContent(R.id.c92_tab1);spec.setIndicator("Clock");tabs.addTab(spec);/* (2)增加第2页 */spec = tabs.newTabSpec("Tag2");spec.setContent(R.id.rll);spec.setIndicator("Button");tabs.addTab(spec);// 步骤3:可通过setCurrentTab(index)指定显示的页,从0开始计算。tabs.setCurrentTab(1);}//动态添加一个Tab页public void onClick(View view){TabHost.TabSpec spec = tabs.newTabSpec("tag3");        spec.setContent(new TabHost.TabContentFactory() {            /*createTabContent将返回View,这里我们简单用一个模拟时钟*/            public View createTabContent(String tag) {                return new AnalogClock(MainActivity.this);            }        });        spec.setIndicator("other");        tabs.addTab(spec);}}
如上button监听中,可以动态地添加一个tab标签


0 0