android之實現底部TabHost

openkk 12年前發布 | 77K 次閱讀 Android Android開發 移動開發

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

<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/tabhost" 
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <RelativeLayout
        android:background="@drawable/g"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">
    <TabWidget android:id="@android:id/tabs"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true">
    </TabWidget>
    <FrameLayout android:id="@android:id/tabcontent"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">
        <RelativeLayout android:id="@+id/tabFirst"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:orientation="vertical">
        <AnalogClock
            android:id="@+id/widget31"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true">
        </AnalogClock>
        </RelativeLayout>>
        <RelativeLayout android:id="@+id/tabSecond"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:orientation="vertical">
            <DigitalClock
                android:layout_centerInParent="true"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content">
            </DigitalClock>
        </RelativeLayout>
    </FrameLayout>
    </RelativeLayout>
</TabHost>
下面是主程序代碼,注釋寫的比較詳細,這里就不在多解釋了
package com.cloay;

import android.app.TabActivity; import android.graphics.Color; import android.os.Bundle; import android.widget.TabHost; import android.widget.TabHost.OnTabChangeListener; import android.widget.Toast;

public class TabTestActivity extends TabActivity { TabHost tabHost; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); //獲取TabHost對象 tabHost = getTabHost();
// tabHost.setup(); //新建一個newTabSpec,設置標簽和圖標(setIndicator),設置內容(setContent) tabHost.addTab(tabHost.newTabSpec("Test one").setIndicator("",getResources().getDrawable(android.R.drawable.ic_menu_call)).setContent(R.id.tabFirst)); tabHost.addTab(tabHost.newTabSpec("Test two").setIndicator("",getResources().getDrawable(android.R.drawable.ic_menu_camera)).setContent(R.id.tabSecond)); //設置TabHost的背景顏色 tabHost.setBackgroundColor(Color.argb(150,22,70,150)); //設置TabHost的背景圖片資源 // tabHost.setBackgroundResource(R.drawable.bg); //設置當前現實哪一個標簽 tabHost.setCurrentTab(0); //0為標簽ID //標簽切換處理,用setOnTabChangedListener
tabHost.setOnTabChangedListener(new OnTabChangeListener(){ public void onTabChanged(String tabId){ Toast.makeText(TabTestActivity.this, "This is a Test!", Toast.LENGTH_LONG).show(); } }); } }</pre>轉自:http://blog.csdn.net/shang_515/article/details/6781163

 本文由用戶 openkk 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!