[英]How to deal with tabs as views?
我已將標簽創建為視圖。 但是我沒有找到有關如何操縱這些視圖的任何信息。 例如,如果我想在一個視圖中顯示一個列表視圖,而在另一個視圖中顯示一個表單(表格布局)。 我可以為每個標簽設置單獨的布局嗎? 而且更重要的是,我在哪里包含有關每個選項卡的Java代碼?
這是我的Java代碼:
public class TabWorkEntryActivity extends TabActivity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tabworkentry);
TabHost mTabHost = getTabHost();
mTabHost.addTab(mTabHost.newTabSpec("top10").setIndicator("Top 10").setContent(R.id.Top_10));
mTabHost.addTab(mTabHost.newTabSpec("billable").setIndicator("Billable").setContent(R.id.Billable));
mTabHost.addTab(mTabHost.newTabSpec("product").setIndicator("Product").setContent(R.id.Product));
mTabHost.addTab(mTabHost.newTabSpec("regular").setIndicator("Regular").setContent(R.id.General));
mTabHost.setCurrentTab(3);
}
任何幫助將不勝感激。
放置內容和處理選項卡的主要方法有兩種:1.創建活動,然后將活動放置在tabHost中。2.使用相同的代碼添加選項卡來處理內容(刪除所有設置內容)總是使用第二種方式,因為我的代碼是有組織的,所以不會太大...
這是一個示例布局,其中內容是另一個布局...
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TabHost
android:id="@+id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TabWidget
android:id="@android:id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</TabWidget>
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:id="@+id/checkall_tab"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<include
android:layout_width="match_parent"
android:layout_height="match_parent"
layout="@layout/checkall" />
</LinearLayout>
<LinearLayout
android:id="@+id/view_tab"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<include
android:layout_width="match_parent"
android:layout_height="match_parent"
layout="@layout/viewall" />
</LinearLayout>
<LinearLayout
android:id="@+id/run_program"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<include
android:layout_width="match_parent"
android:layout_height="match_parent"
layout="@layout/programs" />
</LinearLayout>
</FrameLayout>
</LinearLayout>
</TabHost>
</LinearLayout>
我使用Android 4.0 tabView編寫了此代碼。 嘗試理解它。。。您必須實例化TabHost,然后使用此對象創建選項卡。 然后將選項卡添加到TabHost。 要按ID查找Views,無需在訪問的選項卡上傳遞“視圖”。
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
tabHost = (TabHost) findViewById(R.id.tabhost);
tabHost.setup();
checkAll = tabHost.newTabSpec("checkAll");
viewAll = tabHost.newTabSpec("viewAll");
runProgram = tabHost.newTabSpec("runProgram");
viewAll.setContent(R.id.view_tab);
viewAll.setIndicator("View All");
tabHost.addTab(viewAll);
checkAll.setContent(R.id.checkall_tab);
checkAll.setIndicator("Check All");
tabHost.addTab(checkAll);
runProgram.setContent(R.id.run_program);
runProgram.setIndicator("Run program");
tabHost.addTab(runProgram);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.