【问题标题】:Hello, TabWidget each tab refer to new xml你好,TabWidget 每个选项卡都引用新的xml
【发布时间】:2011-01-06 01:34:27
【问题描述】:

我使用的是 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>

我基本上想做的是让每个选项卡都引用它自己的 xml 文件,而不是 main.xml 中的所有文件,但是第一个选项卡中的文本没有显示出来。

【问题讨论】:

  • 正如您在java文件中的第一个选项卡上方看到的那样,main.xml中的第一个

标签: android xml textview android-tabs refer


【解决方案1】:

您是否要让每个标签都引用其自己的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);
    }
}

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

【讨论】:

  • 感谢您的输入,但无论我尝试多少次,我的应用程序仍然会在它打开之前强制关闭,是的,我想让每个选项卡都引用它自己的 Activity。按照您的示例和您提供的链接中的示例,我的应用程序强制关闭。我什至查找了有关创建新活动的说明,但在 android 开发站点上找不到任何信息。
  • 您是否将您的活动添加到清单中?如果您逐行按照教程进行操作,请尝试找到一个已经完成的简单项目并自己运行,以确保您的应用程序之外的某些内容不存在问题
  • 实际上我并没有将它们添加到我的清单中,在做一些研究时我发现在这里:warriorpoint.com/blog/2009/05/24/… 将它们添加到清单中是必要的。虽然回到 android 开发站点,但我无法找到有关如何将我的特定测试活动添加到清单的信息,因为它不是主要的启动项,而是在选项卡中。 p.s.感谢您迄今为止的帮助和耐心!
  • 查看此链接:developer.android.com/guide/topics/manifest/…。这可能有点矫枉过正,但关键是您需要为应用程序中的每个活动添加类似 &lt;activity android:name="ControllerActivity" android:label="ControllerActivity"/&gt; 的行。
  • 我听从了你的建议,我发现我只需要在清单中添加: 就可以了!我不敢相信在你的帮助下我花了这么多时间才弄明白这么简单的事情,但我对结果很满意。再次感谢sahhhm!
【解决方案2】:

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

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

【讨论】:

    【解决方案3】:
    View view = LayoutInflater.from(HelloTabWidget.this).inflate(R.layout.text);
    

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

    【讨论】:

      猜你喜欢
      • 2018-02-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-01-27
      • 2023-04-09
      • 1970-01-01
      • 2013-01-21
      • 1970-01-01
      相关资源
      最近更新 更多