【发布时间】:2013-02-21 00:21:14
【问题描述】:
我使用 tabHost 在我的应用程序中创建了 4 个选项卡, 它工作正常。 下面我展示了只添加 2 个标签的代码。
public class Home_tab extends TabActivity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.home_main);
Resources ressources = getResources();
TabHost tabHost = getTabHost();
// Android tab
Intent intentAndroid = new Intent().setClass(this, CoalActivity.class);
TabSpec tabSpecAndroid = tabHost
.newTabSpec("Android")
.setIndicator("", ressources.getDrawable(R.drawable.tab_dis))
.setContent(intentAndroid);
// Apple tab
Intent intentApple = new Intent().setClass(this, EnergyActivity.class);
TabSpec tabSpecApple = tabHost
.newTabSpec("Apple")
.setIndicator("", ressources.getDrawable(R.drawable.tab_foc))
.setContent(intentApple);
// add all tabs
tabHost.addTab(tabSpecAndroid);
tabHost.addTab(tabSpecApple);
}
}
这是我的 xml 文件
<?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"
android:background="@drawable/background"
android:scrollbarAlwaysDrawHorizontalTrack="true"
>
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="5dp">
<TabWidget
android:id="@android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="5dp" />
</LinearLayout>
</TabHost>
但我的问题是当我添加更多选项卡时,它会出现在同一个窗口中。 例如,目前我添加了 4 个标签,当我尝试再添加 3 个标签时,所有标签都在同一个窗口中?我只想在同一个窗口中添加 4 个选项卡,其他选项卡只有在我滚动选项卡栏时才会出现 这个怎么解决??
【问题讨论】:
-
不推荐使用 TabHost 是有充分理由的。效果不是很好,不够灵活,很难实现。在此链接上,您可以看到在操作栏中实现选项卡是多么容易:developer.android.com/guide/topics/ui/actionbar.html#Tabs
-
如果有人能解决我的问题,请帮助我............我的标签工作正常,但唯一的问题是每个标签都添加到同一个窗口? ?
标签: android tabs android-tabhost