如何在android中创建水平和垂直标签?

时间:2014-05-14 09:56:28

标签: android tabs

我正在Android中编写代码来创建水平和垂直标签。在我的代码中,我只能创建水平选项卡,这是默认值。

XML代码

<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent">

<LinearLayout
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#cccccc"
    android:padding="5dp">



   <HorizontalScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:scrollbars="none">

        <LinearLayout
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="#cccccc"
            android:padding="5dp">

          <TabWidget
            android:id="@android:id/tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

                <LinearLayout
                    android:orientation="horizontal"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:background="#ffffff"
                    android:padding="5dp">            

                            <GridView 
                                android:id="@+id/gridView1"
                                android:numColumns="10"
                                android:gravity="center"
                                android:layout_marginLeft="0dp"
                                android:layout_marginTop="0dp"
                                android:stretchMode="columnWidth"
                                android:paddingLeft="3dp"
                                android:paddingRight="3dp"
                                android:horizontalSpacing="3dp"
                                android:verticalSpacing="3dp" 
                                android:layout_width="1600dp"
                                android:layout_height="fill_parent"/>

        </LinearLayout>            
                     </LinearLayout> 

              </HorizontalScrollView>

                  <FrameLayout
                          android:id="@android:id/tabcontent"
                          android:layout_width="match_parent"
                          android:layout_height="match_parent"
                          android:padding="5dp" />

                    </LinearLayout>

                  </TabHost>

Java代码:

         package com.example.tabsabc;

         import java.util.ArrayList;

         import android.os.Bundle;
         import android.app.ActionBar;
         import android.app.Activity;
         import android.app.Fragment;
         import android.app.TabActivity;
         import android.view.Menu;
         import android.view.View;
         import android.widget.ArrayAdapter;
         import android.widget.GridView; 
         import android.widget.TabHost;
         import android.widget.TabWidget;
         import android.widget.TextView;

         public class MainActivity extends TabActivity implements TabHost.TabContentFactory {


ArrayList<String> names;

GridView gd;

BoxGrid gdv;

TabWidget tb1;

TabWidget tb2;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);

    gd = (GridView)findViewById(R.id.gridView1);

    //tb1 = (TabWidget)findViewById(R.id.tabs1);

    names = new ArrayList<String>();

    final TabHost tabHost = getTabHost();

    for (int i=1; i <= 10; i++) {
        String name = "Tab " + i;
        tabHost.addTab(tabHost.newTabSpec(name)
                .setIndicator(name)
                .setContent(this));



    }

    for(int i1=0;i1<150;i1++)
    {

        names.add(String.valueOf(i1));

    }

     ArrayAdapter gdv = new ArrayAdapter(this, android.R.layout.simple_list_item_1, names);

     gd.setAdapter(gdv);

}

/** {@inheritDoc} */
public View createTabContent(String tag) {
    final TextView tv = new TextView(this);
    tv.setText("Content for tab with tag " + tag);
    return tv;
}}

所以,这是我上面的代码,我找不到可以在XML布局中添加垂直选项卡的位置。我找不到任何技术来做到这一点。

请告诉我,建议我一些好的解决方案。

1 个答案:

答案 0 :(得分:0)

我将此留在这里,因为这比尝试设置TabWidget更舒适:

Looking for a universal TabHost style that will work on Android, HTC Sense, Samsung, etc. skins

但更好的是,在ViewPager中使用Fragments而不是旧的TabHost + TabWidget方法。无论如何,它是目前在Android中使用Tabs的推荐方式。