2012年1月26日 星期四

Android - TabActivity

TabHost是Android負責產生Tab Layout的類別,它包含了兩個children,分別為TabWidget與TabContent。

TabWidget負責處理與User進行互動的Tab,TabContent是根據所選的Tab來顯示相對應的內容。

實作方法如下:
main.xml
<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/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" >

        <RelativeLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" >

            <HorizontalScrollView
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_toLeftOf="@+id/next_button"
                android:layout_toRightOf="@+id/up_button"
                android:fillViewport="true"
                android:scrollbars="none" >

                <TabWidget
                    android:id="@android:id/tabs"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content" />
            </HorizontalScrollView>

        </RelativeLayout>

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

            <TextView
                android:id="@+id/textview01"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content" />

            <TextView
                android:id="@+id/textview02"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content" />

            <TextView
                android:id="@+id/textview03"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content" />

            <TextView
                android:id="@+id/textview04"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content" />

            <TextView
                android:id="@+id/textview05"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content" />
        </FrameLayout>

    </LinearLayout>

</TabHost>

Tab.java
public class Tab extends TabActivity
{
 TabHost tabhost;

 public void onCreate(Bundle savedInstanceState)
 {
    super.onCreate(savedInstanceState);

      //.xml有使用TabHost時才要寫
    setContentView(R.layout.main);     

      //.xml沒有使用TabHost時才要寫
    //LayoutInflater.from(Tab.this).inflate(R.layout.main, tabhost.getTabContentView(), true);
 
 tabhost = getTabHost();

    tabhost.addTab(tabhost.newTabSpec("tab1").setIndicator("Tab1", getResources().getDrawable(android.R.drawable.ic_btn_speak_now)).setContent(R.id.textview01));

    tabhost.addTab(tabhost.newTabSpec("tab2").setIndicator("Tab2").setContent(R.id.textview02));
tabhost.addTab(tabhost.newTabSpec("tab3").setIndicator("Tab3").setContent(R.id.textview03));
tabhost.addTab(tabhost.newTabSpec("tab4").setIndicator("Tab4").setContent(R.id.textview04));
    tabhost.addTab(tabhost.newTabSpec("tab5").setIndicator("Tab5").setContent(R.id.textview05));
 
    TabWidget tabWidget = tabhost.getTabWidget();


      //取得Tab的數量
    int count = tabWidget.getChildCount();

      //取得手機的螢幕尺寸
    DisplayMetrics displayMetrics = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
    int screenWidth = displayMetrics.widthPixels;
 int screenheight = displayMetrics.heightPixels;

    if (count >= 3)
    {
       for (int i = 0; i < count; i++)
     {
         tabWidget.getChildTabViewAt(i).setMinimumWidth((screenWidth) / 3);
     }
   }

      //設置TabHost點選後的監聽器
    tabhost.setOnClickListener(new TabHostOnClickListener());

      //設置TabHost的Tab切換後的監聽器
    tabhost.setOnTabChangedListener(new TabHostOnTabChangedListener());
 }

  class TabHostOnClickListener implements OnClickListener
 {
@Override
public void onClick(View v)
{
Toast.makeText(Tab.this, "OnClick~", Toast.LENGTH_SHORT).show();
}
 }

 class TabHostOnTabChangedListener implements OnTabChangeListener
 {
@Override
public void onTabChanged(String tab)
{
            //必須要與當初設置Tab的Space名稱相同
if(tab.equals("tab1"))
{
Toast.makeText(Tab.this, "Tab1~", Toast.LENGTH_SHORT).show();
}

if(tab.equals("tab2"))
{
Toast.makeText(Tab.this, "Tab2~", Toast.LENGTH_SHORT).show();
}

if(tab.equals("tab3"))
{
Toast.makeText(Tab.this, "Tab3~", Toast.LENGTH_SHORT).show();
}

if(tab.equals("tab4"))
{
Toast.makeText(Tab.this, "Tab4~", Toast.LENGTH_SHORT).show();
}

if(tab.equals("tab5"))
{
Toast.makeText(Tab.this, "Tab5~", Toast.LENGTH_SHORT).show();
}
}
}
}

執行結果如下:

沒有留言:

張貼留言