0

我正在使用 Google 的Hello, TabWidget 示例,但将其更改为如下所示:

主.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>

我基本上想要做的是让每个选项卡都引用它自己的 xml 文件,而不是 main.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 (或其他任何内容)的类,该类设置test.xml为布局。

public class Test extends Activity {

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

如果这是您尝试设置的场景类型,本教程将进一步帮助您。

于 2011-01-06T01:45:59.837 回答
0

使用内置选项卡主机的选项卡有三个选项。

  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 回答
0
View view = LayoutInflater.from(HelloTabWidget.this).inflate(R.layout.text);

这不太正确,因为 LayoutInflater.inflate 需要两个参数。

于 2011-02-23T10:32:23.870 回答