您的位置:首页 > 其它

TabHost便签的用法

2016-05-05 20:56 239 查看
    TabHost可以产生分栏的效果。具体实现过程:

首先,在布局文件中实现,代码如下:

<LinearLayout 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" >

    <TabHost

        android:id="@+id/tabhost"

        android:layout_width="match_parent"

        android:layout_height="match_parent"

        android:layout_alignParentLeft="true"

        android:layout_alignParentTop="true" >

        <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" >

            </TabWidget>

            <FrameLayout

                android:id="@android:id/tabcontent"

                android:layout_width="match_parent"

                android:layout_height="match_parent" >

                <LinearLayout

                    android:id="@+id/tab1"

                    android:layout_width="match_parent"

                    android:layout_height="match_parent" >

                    <Button

                        android:id="@+id/button1"

                        android:layout_width="wrap_content"

                        android:layout_height="wrap_content"

                        android:text="StartWatch" />

                    <Button

                        android:id="@+id/button2"

                        android:layout_width="wrap_content"

                        android:layout_height="wrap_content"

                        android:text="StopWatch" />

                    <TextView

                        android:id="@+id/textView1"

                        android:layout_width="wrap_content"

                        android:layout_height="wrap_content"

                        android:text="TextView" />

                </LinearLayout>

                <LinearLayout

                    android:id="@+id/tab2"

                    android:layout_width="match_parent"

                    android:layout_height="match_parent" >

                  <TextView

                        android:id="@+id/textView2"

                        android:layout_width="wrap_content"

                        android:layout_height="wrap_content"

                        android:text="TextView" />

                    

                </LinearLayout>

                <LinearLayout

                    android:id="@+id/tab3"

                    android:layout_width="match_parent"

                    android:layout_height="match_parent" >

                     <Button

                        android:id="@+id/bAddTab"

                        android:layout_width="wrap_content"

                        android:layout_height="wrap_content"

                        android:text="Add a Tab" />

                </LinearLayout>

            </FrameLayout>

        </LinearLayout>

    </TabHost>

</LinearLayout>

然后还需要在MainActivity里面写代码才能完全把tabhost的功能实现出来,代码如下:

package com.example.tabhost;

import android.os.Bundle;

import android.app.Activity;

import android.view.Menu;

import android.view.MenuItem;

import android.widget.TabHost;

import android.widget.TabHost.TabSpec;

import android.support.v4.app.NavUtils;

public class MainActivity extends Activity {

    @Override

    public void onCreate(Bundle savedInstanceState)
a1a7
{

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        TabHost th=(TabHost)findViewById(R.id.tabhost);

        th.setup();

        TabSpec specs=th.newTabSpec("tab1");

        specs.setContent(R.id.tab1);

        specs.setIndicator("StopWatch");//设置选项卡的标题

        th.addTab(specs);

        

        specs=th.newTabSpec("tab1");

        specs.setContent(R.id.tab2);

        specs.setIndicator("tab 2");//设置选项卡的标题

        th.addTab(specs);

        

        specs=th.newTabSpec("tab1");

        specs.setContent(R.id.tab3);

        specs.setIndicator("Add a tab");//设置选项卡的标题

        th.addTab(specs);

    }

    @Override

    public boolean onCreateOptionsMenu(Menu menu) {

        getMenuInflater().inflate(R.menu.activity_main, menu);

        return true;

    }

    

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: