天天看點

android實作界面底部的tab

預設的tabhost中的tabwidget是放在頂部的,有時需要将TAB移到底部來,這時需要在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">

<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">

<AnalogClock

android:id="@+id/tab1"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:layout_centerHorizontal="true" />

<Button

android:id="@+id/tab2"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:text="A semi-random button" />

</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="60px" />

</RelativeLayout>

</TabHost>

</LinearLayout> 

我們将tabWidget放到一個relativeLayout中,然後加上這句android:layout_alignParentBottom="true",代碼實作如下

public class TabTest2 extends Activity {

public void onCreate(Bundle icicle) {

super.onCreate(icicle);

setContentView(R.layout.tabtest2);

TabHost tabs=(TabHost)findViewById(R.id.tabhost);

tabs.setup();

TabHost.TabSpec spec=tabs.newTabSpec("tag1");

spec.setContent(R.id.tab1);

spec.setIndicator("Clock");

tabs.addTab(spec);

spec=tabs.newTabSpec("tag2");

spec.setContent(R.id.tab2);

spec.setIndicator("Button");

tabs.addTab(spec);

tabs.setCurrentTab(0);

}

這樣就可以把tab置于頁面底部了,其實跟上次講的LinearLayout的buttonBar樣式有點類似

繼續閱讀