Android界面開發中的面板控件Tab
在手機有限的屏幕里面,有時候我們要安排較多的內容可能無法容納,當然我們可以使用版面的滾動功能,但是這樣做顯得不是很好看,而且有時候我們需要對不同功能集合的控件集中在各自的面板中,這就需要使用面板Tab控件了。
面板控件的好處是能在一個界面上同時顯示不同的面板內容,通過面板標簽方便的切換到不同的面板上面,下面ATAAW.COM開始介紹這個Tab控件的使用,為了直觀起見,我們直接從例子介紹Tab控件的兩種顯示方法。
布局文件中定義兩個Tab中顯示的內容,這里以TextView為例。
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent">
<!– Tab 1 中顯示的內容 –>
<TextView android:id="@+id/content1" android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="tab1 content" />
<!– Tab 2 中顯示的內容 –>
<TextView android:id="@+id/content2" android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="tab2 content" />
</FrameLayout>
在程序中使用Tab并調用布局中的控件:
public class _Tab extends TabActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TabHost tabHost = getTabHost();
LayoutInflater.from(this).inflate(R.layout.tab, tabHost.getTabContentView(), true);
// Tab 1 的內容
tabHost.addTab(tabHost.newTabSpec("tab1")
.setIndicator("tab1", getResources().getDrawable(R.drawable.p1))
.setContent(R.id.view1));
// Tab 2 的內容
tabHost.addTab(tabHost.newTabSpec("tab2")
.setIndicator("tab2", getResources().getDrawable(R.drawable.p1))
.setContent(R.id.view2));
// Tab 3 的內容(用指定的 Activity來設置 Tab 的內容)
tabHost.addTab(tabHost.newTabSpec("tab3")
.setIndicator("tab3", getResources().getDrawable(R.drawable.p1))
.setContent(new Intent(this, _TextView.class)));
}
}
由以上例子可見,Tab的使用主要的方法有addTab、setIndicator、setContent,另外,除了使用布局文件來定義Tab中顯示的內容,還可以直接把另一個Activity作為Tab的內容顯示,使得每個Tab面板的內容獨立開來。
?
鳳凰涅槃/浴火重生/馬不停蹄/只爭朝夕
???? 隱姓埋名/低調華麗/簡單生活/完美人生
posted on 2010-12-29 10:34 poetguo 閱讀(3572) 評論(1) 編輯 收藏 所屬分類: Android