【问题标题】:Tabs in Android not working correctly?Android 中的标签无法正常工作?
【发布时间】: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


【解决方案1】:

查看以下链接,如果它们有助于解决您的问题,请告诉我

Link1

Link2

link3

【讨论】:

  • 您好 Abhinav Singh 非常感谢
  • 嗨,现在我的标签可以滚动了,但现在我有一个问题如何调整每个标签的大小?,
  • 嗨 Abhinav,我添加了 2 张图片,在第一张图片中它只显示 3 个选项卡,当我水平滚动时,第 4、第 5 和第 6 个选项卡完全显示在屏幕上?如何解决这个问题
  • 查看此链接以获取帮助stackoverflow.com/questions/5799320/… 您也可以像 tabHost.getTabWidget().getChildAt(0).getLayoutParams().width = 140; 一样为标签指定宽度;
猜你喜欢
  • 1970-01-01
  • 2014-05-14
  • 1970-01-01
  • 1970-01-01
  • 2011-01-23
  • 2015-10-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多