歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Android之實現底部TabHost

Android之實現底部TabHost

日期:2017/3/1 10:56:05   编辑:Linux編程

先說布局文件,如下:利用Android:layout_alignParentBottom="true" 實現底部顯示

  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:orientation="vertical"
  5. android:layout_width="fill_parent"
  6. android:layout_height="fill_parent">
  7. <RelativeLayout
  8. android:background="@drawable/g"
  9. android:layout_width="fill_parent"
  10. android:layout_height="fill_parent">
  11. <TabWidget android:id="@android:id/tabs"
  12. android:layout_width="fill_parent"
  13. android:layout_height="wrap_content"
  14. android:layout_alignParentBottom="true">
  15. </TabWidget>
  16. <FrameLayout android:id="@android:id/tabcontent"
  17. android:layout_width="fill_parent"
  18. android:layout_height="fill_parent">
  19. <RelativeLayout android:id="@+id/tabFirst"
  20. android:layout_width="fill_parent"
  21. android:layout_height="fill_parent"
  22. android:orientation="vertical">
  23. <AnalogClock
  24. android:id="@+id/widget31"
  25. android:layout_width="wrap_content"
  26. android:layout_height="wrap_content"
  27. android:layout_centerInParent="true">
  28. </AnalogClock>
  29. </RelativeLayout>>
  30. <RelativeLayout android:id="@+id/tabSecond"
  31. android:layout_width="fill_parent"
  32. android:layout_height="fill_parent"
  33. android:orientation="vertical">
  34. <DigitalClock
  35. android:layout_centerInParent="true"
  36. android:layout_width="wrap_content"
  37. android:layout_height="wrap_content">
  38. </DigitalClock>
  39. </RelativeLayout>
  40. </FrameLayout>
  41. </RelativeLayout>
  42. </TabHost>
下面是主程序代碼,注釋寫的比較詳細,這裡就不在多解釋了
  1. package com.cloay;
  2. import android.app.TabActivity;
  3. import android.graphics.Color;
  4. import android.os.Bundle;
  5. import android.widget.TabHost;
  6. import android.widget.TabHost.OnTabChangeListener;
  7. import android.widget.Toast;
  8. public class TabTestActivity extends TabActivity {
  9. TabHost tabHost;
  10. @Override
  11. public void onCreate(Bundle savedInstanceState) {
  12. super.onCreate(savedInstanceState);
  13. setContentView(R.layout.main);
  14. //獲取TabHost對象
  15. tabHost = getTabHost();
  16. // tabHost.setup();
  17. //新建一個newTabSpec,設置標簽和圖標(setIndicator),設置內容(setContent)
  18. tabHost.addTab(tabHost.newTabSpec("Test one").setIndicator("",getResources().getDrawable(android.R.drawable.ic_menu_call)).setContent(R.id.tabFirst));
  19. tabHost.addTab(tabHost.newTabSpec("Test two").setIndicator("",getResources().getDrawable(android.R.drawable.ic_menu_camera)).setContent(R.id.tabSecond));
  20. //設置TabHost的背景顏色
  21. tabHost.setBackgroundColor(Color.argb(150,22,70,150));
  22. //設置TabHost的背景圖片資源
  23. // tabHost.setBackgroundResource(R.drawable.bg);
  24. //設置當前現實哪一個標簽
  25. tabHost.setCurrentTab(0); //0為標簽ID
  26. //標簽切換處理,用setOnTabChangedListener
  27. tabHost.setOnTabChangedListener(new OnTabChangeListener(){
  28. public void onTabChanged(String tabId){
  29. Toast.makeText(TabTestActivity.this, "This is a Test!", Toast.LENGTH_LONG).show();
  30. }
  31. });
  32. }
  33. }
Copyright © Linux教程網 All Rights Reserved