Android開發之-tabHost-1

1.  新建主界面java

<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"
    tools:context=".MainActivity" >

    <TabHost
        android:id="@+id/tabHost"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

        <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/content1"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:background="#ff0000"
                    android:orientation="vertical" >
                </LinearLayout>
                <!-- 第二個標籤 -->
                <LinearLayout
                    android:id="@+id/content2"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:background="#f0f000"
                    android:orientation="vertical" >
                </LinearLayout>
                <!-- 第三個標籤 -->
                <LinearLayout
                    android:id="@+id/content3"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:background="#0000ff"
                    android:orientation="vertical" >
                </LinearLayout>
            </FrameLayout>
        </LinearLayout>
    </TabHost>

</RelativeLayout>

2.新建MainActivityandroid

import android.app.Activity;
import android.os.Bundle;
import android.widget.Button;
import android.widget.TabHost;

public class MainActivity extends Activity
{
	private TabHost tabHost;

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		initViews();
	}
	private void initViews()
	{
		Button button1 = new  Button(this);
		button1.setText("標籤1");
		tabHost = (TabHost) findViewById(R.id.tabHost);
		tabHost.setup();
		tabHost.addTab(tabHost.newTabSpec("tab1").setIndicator(button1).setContent(R.id.content1));
		tabHost.addTab(tabHost.newTabSpec("tab2").setIndicator("標籤2").setContent(R.id.content2));	
		tabHost.addTab(tabHost.newTabSpec("tab3").setIndicator("標籤3").setContent(R.id.content3));	
	}
}

tabHost.newTabSpec("tab1") 裏面參數是爲了tabHost內部區別使用

在這裏tabHost的 id 是使用的自定義的 id 而且 MainActivity是繼承的原生的Activityapp

setIndicator(button1)   使用自定義的 View 做爲標籤形式ide

setIndicator("標籤2")    使用系統默認的tab標籤形式 並設置文字爲:」標籤2「this


下一章將說明第二種tabHost的使用方法。code

相關文章
相關標籤/搜索