android之實現底部TabHost


先說布局文件,如下:利用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 
 3 import android.app.TabActivity;
 4 import android.graphics.Color;
 5 import android.os.Bundle;
 6 import android.widget.TabHost;
 7 import android.widget.TabHost.OnTabChangeListener;
 8 import android.widget.Toast;
 9 
10 public class TabTestActivity extends TabActivity {
11     TabHost tabHost;
12     @Override
13     public void onCreate(Bundle savedInstanceState) {
14         super.onCreate(savedInstanceState);
15         setContentView(R.layout.main);
16         //獲取TabHost對象
17         tabHost = getTabHost();   
18 //        tabHost.setup();
19         //新建一個newTabSpec,設置標簽和圖標(setIndicator),設置內容(setContent)
20         tabHost.addTab(tabHost.newTabSpec("Test one").setIndicator("",getResources().getDrawable(android.R.drawable.ic_menu_call)).setContent(R.id.tabFirst));
21         tabHost.addTab(tabHost.newTabSpec("Test two").setIndicator("",getResources().getDrawable(android.R.drawable.ic_menu_camera)).setContent(R.id.tabSecond));
22         //設置TabHost的背景顏色
23         tabHost.setBackgroundColor(Color.argb(150,22,70,150));
24         //設置TabHost的背景圖片資源
25 //        tabHost.setBackgroundResource(R.drawable.bg);
26         //設置當前現實哪一個標簽
27         tabHost.setCurrentTab(0);   //0為標簽ID
28         //標簽切換處理,用setOnTabChangedListener    
29         tabHost.setOnTabChangedListener(new OnTabChangeListener(){
30             public void onTabChanged(String tabId){
31                 Toast.makeText(TabTestActivity.this, "This is a Test!", Toast.LENGTH_LONG).show();
32             }
33         });
34     }
35 }

 


免責聲明!

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



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