第二种:
在LinerLayout布局里面嵌套FrameLayout和RelativeLayout布局,将TabWidget放置在RelativeLayout里面,之后设置RelativeLayout的Android:layout_alignParentBottom="true" 属性,这个属性的功能是将TabWidget置于父元素(也就是LinerLayout)的底部。这样就能将Tab置于底部了。
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout
- xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent">
- <TabHost
- android:id="@+id/tabhost"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent">
- <FrameLayout
- android:id="@android:id/tabcontent"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:paddingBottom="62px"
- >
- <TextView
- android:id="@+id/tab1"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:text="这是TabOne"
- />
- <TextView
- android:id="@+id/tab2"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:text="这是TabTwo"/>
- </FrameLayout>
- <RelativeLayout
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- >
- <TabWidget
- android:id="@android:id/tabs"
- android:layout_alignParentBottom="true"
- android:layout_width="fill_parent"
- android:layout_height="65.0px"
- android:background="@drawable/tab_bg"
- />
- </RelativeLayout>
- </TabHost>
- </LinearLayout>
main.xml
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- 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="@string/hello"
- />
- </LinearLayout>
TabHostActivity.java
- package com.lingdududu.test;
- import android.app.Activity;
- import android.os.Bundle;
- import android.widget.TabHost;
- public class TabHostActivity extends Activity {
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.tabs);
- TabHost tabs=(TabHost)findViewById(R.id.tabhost);
- tabs.setup();
- TabHost.TabSpec spec=tabs.newTabSpec("tag1");
- spec.setContent(R.id.tab1);
- spec.setIndicator("TabOne");
- tabs.addTab(spec);
- spec=tabs.newTabSpec("tag2");
- spec.setContent(R.id.tab2);
- spec.setIndicator("TabTwo");
- tabs.addTab(spec);
- tabs.setCurrentTab(0);
- }
- }
效果图: