【问题标题】:How can i create Horizontal and Vertical tabs in android?如何在android中创建水平和垂直选项卡?
【发布时间】:2014-05-14 18:06:33
【问题描述】:

我正在 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 布局中添加垂直选项卡的地方。我找不到任何技术可以做到这一点。

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

【问题讨论】:

    标签: android tabs


    【解决方案1】:

    我将把它留在这里,因为这比尝试设置 TabWidget 的样式更舒适:

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

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

    【讨论】:

    • 是的,谢谢,但想法对我来说不是很清楚,只有我能理解我们可以使用 ActionBar Sherlock API 来创建标签...。你能给出一些具体的链接吗?这样我就可以快速到达他们......
    • 尝试使用链接提供的我的答案,您不需要使用ABS,如果您想继续使用TabHost+TabWidget,只需按照说明操作即可。这里还有一个完整的示例项目:einschnaehkeee.blogspot.de
    • 但请记住,Fragment 比 TabHost+TabWidget 更受支持且更易于处理
    猜你喜欢
    • 2020-04-12
    • 2014-01-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多