歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Android開發教程:底部Tab的兩種實現方式

Android開發教程:底部Tab的兩種實現方式

日期:2017/3/1 10:36:25   编辑:Linux編程

第一種:

下面的tabs.xml布局文件中,整個布局是垂直顯示的,分為FrameLayout和TabWidget上下兩部分,在FrameLayout 布局裡面使用layout_weight=“1” ,而TabWidget沒有設置這個屬性,那就默認為0。那麼在這布局中,FrameLayout 就按比例分得整個屏幕的3/4,而沒有設置layout_weight屬性的TabWidget只是占用剛好能顯示自己空間大小的位置。這樣的話,就能達到就Tab置於底部了。

layout_weight具體可以看看http://liangruijun.blog.51cto.com/3061169/632532裡面的FrameLayout 布局

tabs.xml

  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. >
  7. <LinearLayout
  8. android:orientation="vertical"
  9. android:layout_width="fill_parent"
  10. android:layout_height="fill_parent"
  11. >
  12. <FrameLayout
  13. android:id="@android:id/tabcontent"
  14. android:layout_width="fill_parent"
  15. android:layout_height="match_parent"
  16. android:layout_weight="1"
  17. >
  18. <TextView
  19. android:id="@+id/view1"
  20. android:layout_width="wrap_content"
  21. android:layout_height="wrap_content"
  22. android:text="nihao"
  23. />
  24. <TextView
  25. android:id="@+id/view2"
  26. android:layout_width="wrap_content"
  27. android:layout_height="wrap_content"
  28. android:text="nihenhao"
  29. />
  30. </FrameLayout>
  31. <TabWidget
  32. android:id="@android:id/tabs"
  33. android:layout_width="fill_parent"
  34. android:layout_height="wrap_content"
  35. />
  36. </LinearLayout>
  37. </TabHost>

main.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:orientation="vertical"
  4. android:layout_width="fill_parent"
  5. android:layout_height="fill_parent"
  6. >
  7. <TextView
  8. android:layout_width="fill_parent"
  9. android:layout_height="wrap_content"
  10. android:text="@string/hello"
  11. />
  12. </LinearLayout>

TestHostActivity.java

  1. package com.lingdududu.test;
  2. import android.app.TabActivity;
  3. import android.os.Bundle;
  4. import android.widget.TabHost;
  5. public class TestHostActivity extends TabActivity {
  6. /** Called when the activity is first created. */
  7. @Override
  8. public void onCreate(Bundle savedInstanceState) {
  9. super.onCreate(savedInstanceState);
  10. setContentView(R.layout.tabs);
  11. TabHost tabhost = getTabHost();
  12. tabhost.addTab(tabhost.newTabSpec("111").setIndicator("view1").setContent(R.id.view1));
  13. tabhost.addTab(tabhost.newTabSpec("222").setIndicator("view2").setContent(R.id.view2));
  14. }
  15. }

效果:

650) this.width=650;">

Copyright © Linux教程網 All Rights Reserved