在Android中实现标签页的最佳方法? [英] Best way to implement tabs in android?

查看:72
本文介绍了在Android中实现标签页的最佳方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已阅读了一些实现应用页面底部选项卡的代码.而且代码中没有不推荐使用的方法/类,对我来说,这是一种非常简单,干净的实现选项卡的方法.

I have read looked through some code that implements tabs on the bottom of the app's page. And there is no deprecated method/class inside the code, and to me it is a very easy and clean way to implements tabs.

但是我听说实现选项卡的新方法是使用片段.

But I heard the new way of implementing tabs is to use fragments.

那么,哪个更好?为什么呢?

So, which one is better? And why?

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" >

    <TabHost
        android:id="@+id/edit_item_tab_host"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >

        <TabWidget
            android:id="@android:id/tabs"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom" />


            <FrameLayout
                android:id="@android:id/tabcontent"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:paddingTop="65px" >

            <LinearLayout
                android:id="@+id/edit_item_date_tab"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:orientation="vertical"
                android:padding="5px" >

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="date"
                    android:textStyle="bold" />

            </LinearLayout>

            <LinearLayout
                android:id="@+id/edit_item_geocontext_tab"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:orientation="vertical"
                android:padding="5px" >

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="lieu"
                    android:textStyle="bold" />
            </LinearLayout>

            <LinearLayout
                android:id="@+id/edit_item_text_tab"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:orientation="vertical"
                android:padding="5px" >
            </LinearLayout>
        </FrameLayout>
    </TabHost>

</LinearLayout>

MainActivity类:

import android.app.Activity;
import android.os.Bundle;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;

public class MainActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);



        TabHost tab_host = (TabHost) findViewById(R.id.edit_item_tab_host);
        // don't forget this setup before adding tabs from a tabhost using a xml view or you'll get an nullpointer exception
        tab_host.setup(); 

        TabSpec ts1 = tab_host.newTabSpec("TAB_DATE");
        ts1.setIndicator("tab1");
        ts1.setContent(R.id.edit_item_date_tab);
        tab_host.addTab(ts1);

        TabSpec ts2 = tab_host.newTabSpec("TAB_GEO");
        ts2.setIndicator("tab2");
        ts2.setContent(R.id.edit_item_geocontext_tab);
        tab_host.addTab(ts2);

        TabSpec ts3 = tab_host.newTabSpec("TAB_TEXT");
        ts3.setIndicator("tab3");
        ts3.setContent(R.id.edit_item_text_tab);
        tab_host.addTab(ts3);

        tab_host.setCurrentTab(0);




        }
}

推荐答案

TabHost不能包含片段(可以,但是确实很棘手),所以我不建议今天使用它.

A TabHost cannot contain fragments (well, it can but it's really tricky) so I wouldn't recommend to use it today.

片段是解决问题的方法,如果您想实现新的Tab机制(已集成到Android 3.0中可用的新" ActionBar中)并且仍然支持旧的android版本,则可以使用

Fragments are the way to go, and if you want to implement the new Tab mechanism (which is integrated to the "new" ActionBar available on Android 3.0) and still support old android versions there is ActionBarSherlock, an open-source project which facilitate the use of the action bar design pattern across all versions of Android with a single API.

如今,许多受欢迎的应用程序都在使用此项目(包括Google应用程序),因此值得一看.

A lot of popular apps uses this project nowadays (including Google apps) so it's worth looking at.

这篇关于在Android中实现标签页的最佳方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆