在android中实现标签的最佳方式

在android中实现标签的最佳方式

本文介绍了在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.

Fragment 是要走的路,如果你想实现新的 Tab 机制(它被集成到 Android 3.0 上可用的新"ActionBar)并且仍然支持旧的 android 版本有ActionBarSherlock,一个开源项目,通过单一 API 促进在所有 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中实现标签的最佳方式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-06 01:21