2

アプリのページの下部にあるタブを実装するいくつかのコードを読みました。また、コード内に非推奨のメソッド/クラスはありません。私にとっては、タブを実装するための非常に簡単でクリーンな方法です。

しかし、タブを実装する新しい方法はフラグメントを使用することだと聞きました。

それで、どちらが良いですか?なぜ?

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);




        }
}
4

3 に答える 3

2

ATabHostにはフラグメントを含めることはできません (まあ、可能ですが、非常に注意が必要です) ので、現在使用することはお勧めしません。

フラグメントは行くべき道です。新しいタブメカニズム(ActionBarAndroid 3.0で利用可能な「新しい」に統合されています)を実装し、古いAndroidバージョンを引き続きサポートしたい場合は、使用を容易にするオープンソースプロジェクトであるActionBarSherlockがあります。単一の API を使用して、Android のすべてのバージョンでアクション バーのデザイン パターンを実装します。

現在、多くの人気のあるアプリ (Google アプリを含む) がこのプロジェクトを使用しているため、一見の価値があります。

于 2012-07-26T19:04:20.557 に答える
0

フラグメントは、アクティビティの動作またはユーザー インターフェイスの一部を表します。

Android デベロッパー ガイド。

あなたの質問は少し奇妙に表現されているようです。あなたはTabsページの一番下にいることについて尋ねていますが、使用すべきかどうかを尋ねていますFragments。これらは 2 つの異なるトピックです。

はい、使用する必要がありますFragments。これは、Android が取っているルートであり、今後も使用し続けます。

画面Tabsの下部または上部に配置することは、設計上の決定です。ユーザーエクスペリエンスにとってより快適なものに依存しますが、実際にはあまり関係がありませんFragments

于 2012-07-26T18:51:20.013 に答える