2011-05-29 57 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