Android簡單TabHost標籤切換例子(繼承TabActivity)

TabHost爲何物?

直接看圖:




其中實現的源代碼如下:

package com.example.tabchange;

import android.os.Bundle;
import android.app.Activity;
import android.app.TabActivity;
import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TabHost;
import android.widget.TabWidget;
import android.widget.TabHost.OnTabChangeListener;

/*Android TabHost Demo By KevinWu*/
@SuppressWarnings("deprecation")
public class MainActivity extends TabActivity implements OnTabChangeListener {
	private TabHost tabHost;
	private TabWidget tabWidget;
	TabA TabA;
	TabB TabB;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		tabHost = getTabHost();
		tabWidget = tabHost.getTabWidget();
		addTab();
		setClickListener();
		tabHost = getTabHost();
		tabHost.setOnTabChangedListener(this);
	}

	private void setClickListener() {

		tabWidget.getChildAt(0).setOnClickListener(new MyClickListener());
		tabWidget.getChildAt(1).setOnClickListener(new MyClickListener());

	}

	private void addTab() {
		tabHost.addTab(tabHost.newTabSpec("tab1").setIndicator("TabA")
				.setContent(new Intent(MainActivity.this, TabA.class)));

		tabHost.addTab(tabHost.newTabSpec("tab2").setIndicator("TabB")
				.setContent(new Intent(MainActivity.this, TabB.class)));
		for (int i = 0; i < tabWidget.getChildCount(); i++) {
			tabWidget.getChildAt(i).getLayoutParams().height = 70;
		}
	}

	class MyClickListener implements OnClickListener {

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			if (v == tabWidget.getChildAt(0)) {
				tabHost.setCurrentTab(0);

			} else if (v == tabWidget.getChildAt(1)) {
				tabHost.setCurrentTab(1);
			} else if (v == tabWidget.getChildAt(2)) {
				tabHost.setCurrentTab(2);
			}
		}
	}

	public void onTabChanged(String tabId) {
		// TODO Auto-generated method stub
		Activity activity = getLocalActivityManager().getActivity(tabId);
		activity.onWindowFocusChanged(true);
	}

}</span>


另外要在佈局文件中定義Tabhost:

<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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <TabHost
        android:id="@android:id/tabhost"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/exce_imgview_title" >

        <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"
                android:layout_weight="1" >

                <LinearLayout
                    android:id="@+id/tab1"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:orientation="vertical" >
                </LinearLayout>

                <LinearLayout
                    android:id="@+id/tab2"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:orientation="vertical" >
                </LinearLayout>
            </FrameLayout>
        </LinearLayout>
    </TabHost>

</RelativeLayout></span>

整個例子源代碼稍後上傳。


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