我刚刚开始使用Android,我正在做一个简单的应用程序,我希望看起来像这样:
-------------------------------------------
| | | |
| T A B | T A B | T A B |
| SELECTED | | |
|-----------------------------------------|
| |
| VIEW FLIP CONTENT |
| |
| |
| |
| |
| |
| |
| |
| |
-------------------------------------------
也就是说,我有一些标签,每个标签都有一个视图翻转组件。这使他们能够拥有内部“标签”。问题是,通过这种配置,我必须在一个Activity上运行所有内容!此外,我的布局变得越来越大(见附图)。我怎么能解决这个烂摊子?将每个标签作为单个活动运行?
答案 0 :(得分:1)
可以将所有标签放在不同的活动中。为您的父Activity创建简单的布局,如下所示:
<?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