Android中的标签无法正常工作?

时间:2013-03-06 09:29:05

标签: android tabs android-tabhost

我使用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个选项卡,只有当我滚动选项卡栏时才会出现其他选项卡 怎么解决这个?

1 个答案:

答案 0 :(得分:1)

检查以下链接,并告诉我们是否有助于解决您的问题

Link1

Link2

link3

相关问题