简体   繁体   English

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

[英]Tabs in Android not working correctly?

I have created 4 tabs in my application using tabHost , it working fine. 我已经使用tabHost在应用程序中创建了4个选项卡,它工作正常。 below i show my code for adding only 2 tabs. 下面,我显示仅添加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);

                }
              }

This is my xml file 这是我的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>

But my problem is when i add more tabs, it comes in the same window. 但是我的问题是,当我添加更多标签时,它出现在同一窗口中。 for example, currently i added 4 tabs, when i try to add 3 more tabs , all the tabs are coming in the same window? 例如,当前我添加了4个标签,当我尝试添加3个以上的标签时,所有标签都位于同一窗口中? i want to add only 4 tabs in the same window, other tabs should appears only when i scroll the tab bar How to solve this?? 我只想在同一窗口中添加4个标签,其他标签仅在我滚动标签栏时才出现。

Check following links and let me know if they helps in solving your problem 检查以下链接,让我知道它们是否有助于解决您的问题

Link1 链接1

Link2 LINK2

link3 LINK3

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM