这是我第一个针对Android的选项卡应用程序。我在Androids网站上浏览了“ hellotabwidget”应用程序,但我不知道如何在选项卡中添加内容。 FRAMELAYOUT堆叠在左上方(根据我阅读的内容)。我添加了几个文本视图和一个ImageView,但它仅显示最后一个项目。有没有一种方法可以使用线性布局而不是帧布局?如果没有,如何在标签中放置多个视图?我与示例不同的唯一一件事是我添加了一个第4个选项卡。在其中一项标签活动中,我插入了以下代码,以尝试显示多个项目:

public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);    

        TextView textview = new TextView(this);
        textview.setText("This is the About tab");
        setContentView(textview);

        TextView textview2 = new TextView(this);
        textview2.setText("About Test");
        setContentView(textview2);

        ImageView imgView = new ImageView(this);
        imgView.setImageDrawable(getResources().getDrawable(R.drawable.header));
        setContentView(imgView);
    }

这是我遵循的示例的链接:http://developer.android.com/resources/tutorials/views/hello-tabwidget.html

有帮助吗?

解决方案

您的布局XML文件是什么?我使用此功能,可以在每个选项卡中堆叠几个文本视图。表:

<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/tabhost" 
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <ScrollView android:layout_width="fill_parent" 
        android:layout_height="wrap_content" >
        <LinearLayout android:layout_width="fill_parent" 
            android:layout_height="fill_parent"
            android:orientation="vertical" >
            <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" />
        </LinearLayout>
    </ScrollView>
</TabHost>

在选项卡中的活动:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"
    android:orientation="vertical" >
    <TextView android:id="@+id/textOne" 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
    <TextView android:id="@+id/textTwo" 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
<LinearLayout>

我本人是新手,所以可能有更好的方法来做到这一点。不过,这对我有用。

其他提示

您没有仔细阅读示例。看看第6点;您会看到类似的东西:

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

    Resources res = getResources(); // Resource object to get Drawables
    TabHost tabHost = getTabHost();  // The activity TabHost
    TabHost.TabSpec spec;  // Resusable TabSpec for each tab
    Intent intent;  // Reusable Intent for each tab

    // Create an Intent to launch an Activity for the tab (to be reused)
    intent = new Intent().setClass(this, ArtistsActivity.class);

    // Initialize a TabSpec for each tab and add it to the TabHost
    spec = tabHost.newTabSpec("artists").setIndicator("Artists",
                      res.getDrawable(R.drawable.ic_tab_artists))
                  .setContent(intent);
    tabHost.addTab(spec);

    // Do the same for the other tabs
    intent = new Intent().setClass(this, AlbumsActivity.class);
    spec = tabHost.newTabSpec("albums").setIndicator("Albums",
                      res.getDrawable(R.drawable.ic_tab_albums))
                  .setContent(intent);
    tabHost.addTab(spec);

    intent = new Intent().setClass(this, SongsActivity.class);
    spec = tabHost.newTabSpec("songs").setIndicator("Songs",
                      res.getDrawable(R.drawable.ic_tab_songs))
                  .setContent(intent);
    tabHost.addTab(spec);

    tabHost.setCurrentTab(2);
}

那就是你对 TabActivity. 。如您所见,它有3个活动。您正在做的只是使用一项活动并设置内容视图3次,这显然是错误的。

TabHost.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top