android中tabhost的使用

佈局總共有兩種方法,第一種就是xml佈局,還有就是自定義佈局

<span style="font-size:18px;"><TabHost xmlns:android="http://schemas.android.com/apk/res/android"
    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" >

        </TabWidget>
        <!-- 與源代碼的id一樣 -->

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

            <!-- 這是第一個佈局即使首頁 -->

            <LinearLayout
                android:id="@+id/page1"
                android:layout_width="match_parent"
                android:layout_height="match_parent" >

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:text="這是第一個標籤頁" >
                </TextView>
            </LinearLayout>
            <!-- 這是第二頁 -->

            <LinearLayout
                android:id="@+id/page2"
                android:layout_width="match_parent"
                android:layout_height="match_parent" >

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:text="這是第2個標籤頁" >
                </TextView>
            </LinearLayout>
            <!-- 這是第三頁 -->

            <LinearLayout
                android:id="@+id/page3"
                android:layout_width="match_parent"
                android:layout_height="match_parent" >

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:text="這是第3個標籤頁" >
                </TextView>
            </LinearLayout>
        </FrameLayout>
    </LinearLayout>

</TabHost></span>
Activity是

<span style="font-size:14px;">package cc.tabhost;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;
import android.widget.TextView;

public class MainActivity extends Activity {

	private TabHost tabhost;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		tabhost = (TabHost) this.findViewById(R.id.tabhost);
		tabhost.setup();// 首先找到tabwidget與TabContent兩個控件
		TabSpec tabSpec = tabhost.newTabSpec("page1");// 設置標記。以後可以通過該標記找到該頁
		tabSpec.setIndicator("首頁", getResources().getDrawable(R.drawable.i1));// 設置顯示的字,與圖片
		// tabSpec.setIndicator(createTabview("首頁"));//自定義佈局用的tabSpec.setIndicator(View
		// v)
		tabSpec.setContent(R.id.page1);
		tabhost.addTab(tabSpec);
		tabSpec = tabhost.newTabSpec("page2");
		tabSpec.setIndicator("第二頁", getResources().getDrawable(R.drawable.i2));
		// tabSpec.setIndicator(createTabview("第二頁"));
		tabSpec.setContent(R.id.page2);
		tabhost.addTab(tabSpec);
		tabSpec = tabhost.newTabSpec("page3");
		tabSpec.setIndicator("第三頁", getResources().getDrawable(R.drawable.i3));
		// tabSpec.setIndicator(createTabview("第三頁"));//自定義tabcontent佈局
		tabSpec.setContent(R.id.page3);
		tabhost.addTab(tabSpec);
		tabhost.setCurrentTab(0);// 選擇停留到第一個頁爲首頁
	}

	/**
	 * 自定義佈局,
	 * 
	 * @param 顯示的首頁
	 *            ,第二頁。。
	 * @return 一個view
	 */
	private View createTabview(String string) {
		View v = getLayoutInflater().inflate(R.layout.tab, null);// 找到自定義的tab佈局
		TextView tv = (TextView) (v.findViewById(R.id.name));
		tv.setText(string);
		return v;
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

}
</span>
以上有自定義佈局與xml兩種方法的代碼,以下是自定義布句的tab.xml

<span style="font-size:14px;"><?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/tab_bg"
        android:gravity="center"
        android:textSize="18sp" />

</LinearLayout></span>





發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章