Android入門第十一篇之TabHost,TabWidget

來源:互聯網
上載者:User

這回要介紹的是Android的Tab控制項,Tab控制項可以達到分頁的效果,讓一個螢幕的內容盡量豐富,當然也會增加開發的複雜程度,在有必要的時候再使用。Android的Tab控制項使用起來有點奇怪,必須包含和按照以下的順序:

TabHost控制項->TabWidget(必須命名為tabs)->FrameLayout(必須命名為tabcontent)。

接下來貼出本例啟動並執行:

 

main.xml的源碼:

 view plaincopy to clipboardprint?
<?xml version="1.0" encoding="utf-8"?> 
<TabHost android:layout_width="fill_parent" 
    android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android" android:id="@android:id/TabHost1"> 
    <TabWidget android:id="@android:id/tabs" 
        android:layout_height="wrap_content" android:layout_width="fill_parent"> 
</TabWidget> 
    <FrameLayout android:id="@android:id/tabcontent" 
        android:paddingTop="65px" android:layout_width="fill_parent" android:layout_height="fill_parent"> 
        <LinearLayout android:layout_height="wrap_content" android:id="@+id/Tab1" android:orientation="vertical" android:layout_width="fill_parent"> 
           <EditText android:layout_height="wrap_content" android:id="@+id/edtTab1" android:layout_width="fill_parent"></EditText> 
           <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/btnTab1" android:text="Tab1"></Button> 
        </LinearLayout> 
        <LinearLayout android:layout_height="wrap_content" android:id="@+id/Tab2" android:layout_width="fill_parent" android:orientation="horizontal"> 
           <EditText android:layout_height="wrap_content" android:id="@+id/edtTab2" android:layout_width="wrap_content" android:layout_weight="300"></EditText> 
           <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/btnTab2" android:text="Tab2"></Button></LinearLayout> 
    </FrameLayout> 
</TabHost> 
<?xml version="1.0" encoding="utf-8"?>
<TabHost android:layout_width="fill_parent"
    android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android" android:id="@android:id/TabHost1">
    <TabWidget android:id="@android:id/tabs"
        android:layout_height="wrap_content" android:layout_width="fill_parent">
</TabWidget>
    <FrameLayout android:id="@android:id/tabcontent"
        android:paddingTop="65px" android:layout_width="fill_parent" android:layout_height="fill_parent">
        <LinearLayout android:layout_height="wrap_content" android:id="@+id/Tab1" android:orientation="vertical" android:layout_width="fill_parent">
           <EditText android:layout_height="wrap_content" android:id="@+id/edtTab1" android:layout_width="fill_parent"></EditText>
           <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/btnTab1" android:text="Tab1"></Button>
        </LinearLayout>
        <LinearLayout android:layout_height="wrap_content" android:id="@+id/Tab2" android:layout_width="fill_parent" android:orientation="horizontal">
           <EditText android:layout_height="wrap_content" android:id="@+id/edtTab2" android:layout_width="wrap_content" android:layout_weight="300"></EditText>
           <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/btnTab2" android:text="Tab2"></Button></LinearLayout>
    </FrameLayout>
</TabHost>

程式源碼:

view plaincopy to clipboardprint?
package com.testTab;  
import android.app.TabActivity;  
import android.os.Bundle;  
import android.view.View;  
import android.widget.Button;  
import android.widget.EditText;  
import android.widget.TabHost;  
import android.widget.TabHost.TabSpec;  
public class testTab extends TabActivity {//基於TabActivity構建  
      
    Button btnTab1,btnTab2;  
    EditText edtTab1,edtTab2;  
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.main);  
          
        TabHost tabs = getTabHost();  
        //設定Tab1  
        TabSpec tab1 = tabs.newTabSpec("tab1");  
        tab1.setIndicator("tab1");      // 設定tab1的名稱  
        tab1.setContent(R.id.Tab1);    // 關聯控制項  
        tabs.addTab(tab1);                // 添加tab1  
          
        btnTab1=(Button)this.findViewById(R.id.btnTab1);  
        edtTab1=(EditText)this.findViewById(R.id.edtTab1);  
        btnTab1.setOnClickListener(new ClickEvent());  
          
        //設定Tab2  
        TabSpec tab2 = tabs.newTabSpec("tab2");  
        tab2.setIndicator("tab2");        
        tab2.setContent(R.id.Tab2);      
        tabs.addTab(tab2);                  
  &nb

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.