android tab選項卡效果

來源:互聯網
上載者:User

http://www.javaeye.com/topic/465371

在main.xml或者自己定義的*.xml檔案中建立一個TabHost,需要兩個元素TabWidget和FrameLayout 通常會把這兩個元素放到LinearLayout中。FrameLayout作為改變內容content用的。


注意:TabWidget和FrameLayout 有不同的ID命名空間android:id="@android:id/idnames",這個是必須的因此TabHost才能自動找到它,Activity需要繼承TabActivity。

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

 

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <TabHost xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:id="@android:id/tabhost"  
  4.     android:layout_width="fill_parent"  
  5.     android:layout_height="fill_parent">  
  6.     <LinearLayout  
  7.         android:orientation="vertical"  
  8.         android:layout_width="fill_parent"  
  9.         android:layout_height="fill_parent">  
  10.         <TabWidget  
  11.             android:id="@android:id/tabs"  
  12.             android:layout_width="fill_parent"  
  13.             android:layout_height="wrap_content" />  
  14.         <FrameLayout  
  15.             android:id="@android:id/tabcontent"  
  16.             android:layout_width="fill_parent"  
  17.             android:layout_height="fill_parent">  
  18.             <TextView    
  19.                 android:id="@+id/textview1"  
  20.                 android:layout_width="fill_parent"  
  21.                 android:layout_height="fill_parent"    
  22.                 android:text="this is a tab" />  
  23.             <TextView    
  24.                 android:id="@+id/textview2"  
  25.                 android:layout_width="fill_parent"  
  26.                 android:layout_height="fill_parent"    
  27.                 android:text="this is another tab" />  
  28.             <TextView    
  29.                 android:id="@+id/textview3"  
  30.                 android:layout_width="fill_parent"  
  31.                 android:layout_height="fill_parent"    
  32.                 android:text="this is a third tab" />  
  33.         </FrameLayout>  
  34.     </LinearLayout>  
  35. </TabHost>  

public class HelloTabWidget extends TabActivity  

  1. public void onCreate(Bundle savedInstanceState) {   
  2.     super.onCreate(savedInstanceState);   
  3.     setContentView(R.layout.main);   
  4.   
  5.     mTabHost = getTabHost();   
  6.        
  7.     mTabHost.addTab(mTabHost.newTabSpec("tab_test1").setIndicator("TAB 1").setContent(R.id.textview1));   
  8.     mTabHost.addTab(mTabHost.newTabSpec("tab_test2").setIndicator("TAB 2").setContent(R.id.textview2));   
  9.     mTabHost.addTab(mTabHost.newTabSpec("tab_test3").setIndicator("TAB 3").setContent(R.id.textview3));   
  10.        
  11.     mTabHost.setCurrentTab(0);   
  12. }  

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.