2011-05-29 58 views
1

我剛開始使用Android,我做一個簡單的應用程序,我想是這樣的:問題有巨大的Android佈局

------------------------------------------- 
|    |    |    | 
| T A B | T A B | T A B | 
| SELECTED |    |    | 
|-----------------------------------------| 
|           | 
| VIEW FLIP CONTENT      | 
|           | 
|           | 
|           | 
|           | 
|           | 
|           | 
|           | 
|           | 
------------------------------------------- 

也就是說,我有一些選項卡,每個人有一個觀點翻轉零件。這使他們有內部的「標籤」。問題是,通過這種配置,我必須在單個Activity上運行所有內容!此外,我的佈局變得越來越龐大(見附圖)。我該如何解決這個爛攤子?將每個標籤作爲單個活動運行?

layout getting huge

回答

1

它可以把所有的標籤在不同的活動。創建簡單的佈局爲你的父母的活動,這樣的事情:

<?xml version="1.0" encoding="utf-8"?> 
<TabHost xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@android:id/tabhost" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 
    <LinearLayout 
     android:orientation="vertical" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:padding="5dp"> 
     <FrameLayout 
      android:id="@android:id/tabcontent" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:layout_weight="1" /> 
     <TabWidget 
      android:id="@android:id/tabs" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" />  
    </LinearLayout> 
</TabHost> 

現在你已經創造了你想要的標籤佈局,現在是時候你想要的活動,以填補你的標籤。這是在TabActivity的onCreate方法中完成的。

public class MyTabActivity extends TabActivity { 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     Resources res = getResources(); // Resource object to get Drawables 
     TabHost tabHost = getTabHost(); // The activity TabHost 
     TabHost.TabSpec spec; // Resusable TabSpec for each tab 
     Intent intent; // Reusable Intent for each tab 

     // Create an Intent to launch an Activity for the tab (to be reused) 
     intent = new Intent().setClass(this, YourFirstActivity.class); 


     // Initialize a TabSpec for each tab and add it to the TabHost 
     spec = tabHost.newTabSpec("NameOfSpec").setIndicator("TabText1"),null).setContent(intent); 
     tabHost.addTab(spec); 

     intent = new Intent().setClass(this, YourSecondActivity.class); 

     spec = tabHost.newTabSpec("NameOfSpec2").setIndicator("TabText2",null).setContent(intent); 
     tabHost.addTab(spec); 

     tabHost.setCurrentTab(0); 
    } 
} 

這就是它,現在你有一個帶有2個選項卡的tabhost,添加更多隻是添加更多的tabhosts。當然,您需要更改每個選項卡的文本,並填寫自己的活動,而不是「YourFirstActivity」和「YourSecondActivity」。

對於一個教程,看看這裏:http://developer.android.com/resources/tutorials/views/hello-tabwidget.html