Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/234.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android中的可编程选项卡布局_Android_Android Layout_Tabs - Fatal编程技术网

Android中的可编程选项卡布局

Android中的可编程选项卡布局,android,android-layout,tabs,Android,Android Layout,Tabs,我需要在Android应用程序中创建一个选项卡布局,如下所示: TabHost host = (TabHost) root.findViewById(R.id.tab_host); host.setup(); //First tab TabSpec spec = host.newTabSpec("Tab One"); spec.setContent(R.id.tab_one_container); spec.setIndicator("One")

我需要在Android应用程序中创建一个选项卡布局,如下所示:

    TabHost host = (TabHost) root.findViewById(R.id.tab_host);
    host.setup();

    //First tab
    TabSpec spec = host.newTabSpec("Tab One");
    spec.setContent(R.id.tab_one_container);
    spec.setIndicator("One");
    host.addTab(spec);

    //Second tab
    spec = host.newTabSpec("Tab Two");
    spec.setContent(R.id.tab_two_container);
    spec.setIndicator("Two");
    host.addTab(spec);

    //Third tab
    spec = host.newTabSpec("Tab Three");
    spec.setContent(R.id.tab_three_container);
    spec.setIndicator("Three");
    host.addTab(spec);

    //etc...

            //Set the default tab to the first one
    host.setCurrentTab(0);

我有两个要求:

  • 以编程方式填充布局的能力,而不仅仅是从 XML
  • 样式化(我想将ot样式化以获得呈现的效果 上文)

  • 是否有Android提供的或我可以使用的外部库?

    您可以通过编程设置选项卡。大概是这样的:

        TabHost host = (TabHost) root.findViewById(R.id.tab_host);
        host.setup();
    
        //First tab
        TabSpec spec = host.newTabSpec("Tab One");
        spec.setContent(R.id.tab_one_container);
        spec.setIndicator("One");
        host.addTab(spec);
    
        //Second tab
        spec = host.newTabSpec("Tab Two");
        spec.setContent(R.id.tab_two_container);
        spec.setIndicator("Two");
        host.addTab(spec);
    
        //Third tab
        spec = host.newTabSpec("Tab Three");
        spec.setContent(R.id.tab_three_container);
        spec.setIndicator("Three");
        host.addTab(spec);
    
        //etc...
    
                //Set the default tab to the first one
        host.setCurrentTab(0);
    
    在xml中,还需要设置选项卡。像这样:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:paddingLeft="16dp"
    android:paddingRight="16dp" >
    
    <TabHost
        android:id="@+id/tab_host"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="25dp" >
    
        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical" >
    
            <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="wrap_content" >
    
                <LinearLayout
                    android:id="@+id/tab_one_container"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:orientation="vertical"
                    android:text="One" >
    
                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_gravity="right"
                        android:layout_marginLeft="20dp"
                        android:text="Test1" />
                </LinearLayout>
    
                <LinearLayout
                    android:id="@+id/tab_two_container"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:orientation="vertical"
                    android:text="Two" >
    
                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_gravity="right"
                        android:layout_marginLeft="20dp"
                        android:text="Test2" />
                </LinearLayout>
    
                <LinearLayout
                    android:id="@+id/tab_three_container"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:orientation="vertical"
                    android:text="Three" >
    
                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_gravity="right"
                        android:layout_marginLeft="20dp"
                        android:text="Test3" />
                </LinearLayout>
            </FrameLayout>
        </LinearLayout>
    </TabHost>
    
    </LinearLayout>
    
    
    
    这将设置您的选项卡


    关于样式设置,我确信有很多自定义库,但是为了得到您想要的,您可以创建自己的自定义样式。请参考此处作为起点-

    我需要了解的另一件事是如何以编程方式添加
    tab\u One\u容器(以及类似的
    LinearLayout
    s)。这可能有点复杂。。。原因是容器包含选项卡内容的布局,因此所有的文本视图、线性布局等。我不愿意以编程方式向您提供建议,因为我不确定如何做到这一点。对不起,这就是我问这个问题的原因,不过没关系,我会找到答案的。