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();
}
}
}
}

執行結果如下:

地心冒險2:神秘島

今天去中壢威尼斯影城看 "地心冒險2:神秘島",帶著3D眼鏡看電影,感覺真的很棒。

以下是地心冒險2:神秘島的介紹
《地心冒險2:神秘島》敘述17歲的少年尚恩(喬許哈契遜飾),無預警收到一連串編碼求救訊號,但是遍尋航海地圖,在發出訊息的座標的方位上卻找不到任何島嶼的痕跡,然而他深信這個訊息是千真萬確,認定他那愛冒險的爺爺發出來的。
尚恩決定啟程尋覓他那個身在太平洋中某處的爺爺,看到尚恩一副勢在必行的樣子,繼父漢克(巨石強森飾)也只好加入這趟探險,而同行的還有見錢眼開的直昇機飛行員(路易古茲曼)及其美麗的女兒(凡妮莎哈金斯),在艱辛的乘風破浪後,出現在他們的眼前的竟是一座完全超乎人類感官經驗的奇異島嶼,諸如巨大如恐龍般的蜥蜴、小到可以抱在胸前當寵物的大象等,許多不可思議的生物、火山、黃金礦山和若干驚人的祕密,及更多意想不到的驚奇旅程正在前方等著他們。他們將要在大地震造成島嶼下沉、寶藏不見天日前拯救落單居民與平安逃離,如此艱鉅的任務有可能成功達成嗎?
這部由巨石強森、喬許哈契遜與凡妮莎哈金斯所主演的動作家庭冒險動作續集電影結合了最新的3D拍攝技術,除了古文明的建築與怪異比例的野生動物看起來栩栩如生外,也讓巨石強森的胸肌與手臂的肌肉線條在3D的效果下表露無遺,在拍攝過程中,身兼製片的巨石強森有不少無厘頭的鬼點子出現,甚至拿出看家絕活「抖胸示愛」來娛樂大家,胸部肌肉可以分開或同時作出跳動的高難度動作,還能夠配合講話的速度,不只讓工作人員笑翻和紓壓,最後更被導演設計出現在電影橋段中。