0

私は Google のHello, TabWidgetの例を使用していますが、次のように変更しました。

main.xml:

<LinearLayout
    android:orientation="vertical"
    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" />
        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent">
            <TextView 
                android:text="@+layout/text"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent" />
            <TextView 
                android:id="@+id/textview2"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent" 
                android:text="this is another tab" />
            <TextView 
                android:id="@+id/textview3"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent" 
                android:text="this is a third tab" />
        </FrameLayout>
    </LinearLayout>
</TabHost>

Java ファイル:

public class HelloTabWidget extends TabActivity {
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        TabHost mTabHost = getTabHost();

        mTabHost.addTab(mTabHost.newTabSpec("tab_test1").setIndicator("TAB 1").setContent(R.layout.text));
        mTabHost.addTab(mTabHost.newTabSpec("tab_test2").setIndicator("TAB 2").setContent(R.id.textview2));
        mTabHost.addTab(mTabHost.newTabSpec("tab_test3").setIndicator("TAB 3").setContent(R.id.textview3));

        mTabHost.setCurrentTab(0);
    }
}

res/layout の text.xml は次のとおりです。

<LinearLayout
    android:id="@+id/LinearLayout01"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    xmlns:android="http://schemas.android.com/apk/res/android" >

    <TextView
            android:layout_height="wrap_content"
            android:layout_width="fill_parent"
            android:text="This is Tab 1" />
</LinearLayout>

私が基本的にしようとしているのは、各タブが main.xml のすべてではなく独自の xml ファイルを参照するようにすることですが、最初のタブのテキストは表示されません。

4

3 に答える 3

0

各タブに独自のタブを参照させようとしていますActivityか? その場合、各タブのコンテンツとしてインテントを設定できます。

intent = new Intent().setClass(this, Test.class);       
spec = tabHost.newTabSpec("tab_test1").setIndicator("TAB 1").setContent(intent);
        tabHost.addTab(spec);

もちろん、test.xmlレイアウトとして設定する Test (またはその他のもの) という名前のクラスが必要です。

public class Test extends Activity {

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.test);
    }
}

このチュートリアルは、これがセットアップしようとしているシナリオのタイプである場合にさらに役立ちます。

于 2011-01-06T01:45:59.837 に答える
0
View view = LayoutInflater.from(HelloTabWidget.this).inflate(R.layout.text);

LayoutInflater.inflate は 2 つのパラメーターを取るため、これは正しくありません。

于 2011-02-23T10:32:23.870 に答える
0

組み込みのタブ ホストを使用するタブには 3 つのオプションがあります。

  1. 1 つのレイアウト ファイルで 1 つのアクティビティの複数のビュー
  2. 複数の活動
  3. TabContentFactory に組み込まれたカスタム ビュー

#3が欲しいようです。あなたがする必要があるのは次のようなものです:

setContent(new TabHost.TabContentFactory() {
    public View createTabContent(String tag)
    {
        View view = LayoutInflater.from(HelloTabWidget.this).inflate(R.layout.text);
        // Setup the view here
        return view;
    }});
于 2011-01-06T04:36:57.887 に答える