Android之TabHost布局


1.概念

      盛放Tab的容器就是TabHost。TabHost的實現有兩種方式:

      第一種繼承TabActivity,從TabActivity中用getTabHost()方法獲取TabHost。各個Tab中的內容在布局文件中定義就行了。

      第二種方式,不繼承TabActivity,在布局文件中定義TabHost即可,但是TabWidget的id必須是@android:id/tabs,FrameLayout的id必須是@android:id/tabcontent。

 2.案例

1)繼承TabActivity

res/layout/main.xml

<?xml version="1.0" encoding="utf-8"?>
<!-- 定義TabHost組件 -->
<TabHost xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent">
  <!-- 定義第一個標簽頁的內容 -->
  <LinearLayout android:id="@+id/tab01" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent">
    <!-- 定義兩個TextView用於顯示標簽頁中的內容 -->
    <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="孫悟空-2011/07/12"/>
    <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="豬八戒-2011/07/10"/>
  </LinearLayout>
  <!-- 定義第二個標簽頁的內容 -->
  <LinearLayout android:id="@+id/tab02" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent">
    <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="薩僧-2011/07/11"/>
    <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="唐僧-2011/07/10"/>
  </LinearLayout>
  <!-- 定義第三個標簽頁的內容 -->
  <LinearLayout android:id="@+id/tab03" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent">
    <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="孫悟空-2011/07/12"/>
    <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="薩僧-2011/07/08"/>
  </LinearLayout>
</TabHost>

HelloTabHost.java

 public class HelloTabHost extends TabActivity {

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

    //調用TabActivity的getTabHost()方法獲取TabHost對象
    TabHost tabHost = getTabHost();

    //設置使用TabHost布局
    LayoutInflater.from(this).inflate(R.layout.main, tabHost.getTabContentView(),true);

     //添加第一個標簽頁
    tabHost.addTab(tabHost.newTabSpec("tab01").setIndicator("已接電話").setContent(R.id.tab01));

    //添加第二個標簽頁,並在其標簽上添加一個圖片
    tabHost.addTab(tabHost.newTabSpec("tab02").setIndicator("未接電話",getResources().getDrawable(R.drawable.icon)).setContent(R.id.tab02));

    //添加第三個標簽頁
    tabHost.addTab(tabHost.newTabSpec("tab03").setIndicator("已撥電話").setContent(R.id.tab03));
  }
 }

運行效果:

2)不繼承TabActivity

     繼承普通Activity,<TabWidget>標簽id必須為tabs、<FrameLayout>標簽id必須為tabcontent.這個方式在通過findViewById獲得TabHost之后,必須要調用setup方法。

main.xml代碼
<?xml version="1.0" encoding="utf-8"?>  
<!-- TabHost必須包含一個 TabWidget和一個FrameLayout-->
<TabHost android:id="@+id/tabhost" android:layout_width="fill_parent" android:layout_height="wrap_content">
<LinearLayout android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent">
<!-- TabWidget的id屬性必須為 @android:id/tabs-->
<TabWidget android:id="@android:id/tabs" android:orientation="horizontal" android:layout_width="fill_parent"
android:layout_height
="wrap_content" />
<!-- FrameLayout的id屬性必須為 @android:id/tabcontent-->
<FrameLayout android:id="@android:id/tabcontent" android:layout_width="fill_parent" android:layout_height="fill_parent">
<TextView android:id="@+id/view1" android:layout_width="fill_parent" android:layout_height="fill_parent"/>
<TextView android:id="@+id/view2" android:layout_width="fill_parent" android:layout_height="fill_parent"/>
<TextView android:id="@+id/view3" android:layout_width="fill_parent" android:layout_height="fill_parent"/>
</FrameLayout>
</LinearLayout>
</TabHost>
</LinearLayout>

Java代碼

public class TabHostTest extends Activity {  
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// 獲取TabHost對象
TabHost tabHost = (TabHost) findViewById(R.id.tabhost);
// 如果沒有繼承TabActivity時,通過該種方法加載啟動tabHost
tabHost.setup();
tabHost.addTab(tabHost.newTabSpec("tab1").setIndicator("第一個標簽",
getResources().getDrawable(R.drawable.icon)).setContent(
R.id.view1));

tabHost.addTab(tabHost.newTabSpec("tab3").setIndicator("第三個標簽")
.setContent(R.id.view3));

tabHost.addTab(tabHost.newTabSpec("tab2").setIndicator("第二個標簽")
.setContent(R.id.view2));
}
}

效果圖

 --------------------------------------------------------------------

PS: 歡迎關注公眾號"Devin說",會不定期更新Java相關技術知識。

--------------------------------------------------------------------

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM