将相同布局的多个实例扩展到选项卡中

时间:2011-10-23 11:05:04

标签: android tabs inflate

这是一个困扰我几天的问题。我已尽最大努力寻找解决方案,但无论我走到哪里,都说我正在使用的方法是正确的。

情况很简单:我有五个选项卡,它们包含相同的布局,内容略有不同(例如,使用setText更改其中一个标签)。因此,我正在对布局进行五次充气,并将所有这些作为单独的标签添加。

这是我的代码:

package com.test;

import android.app.TabActivity;
import android.content.res.Resources;
import android.os.Bundle;
import android.view.View;
import android.widget.TabHost;
import android.widget.TextView;

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

        TabHost host = getTabHost();
        Resources res = getResources();

        String[] days = { "Monday", "Tuesday", "Wednesday", "Thursday", "Friday" };

        for (int i = 0; i < days.length; i++)
        {
            View contents = getLayoutInflater().inflate(R.layout.test, host.getTabContentView(), true);

            ((TextView)contents.findViewById(R.id.title)).setText(days[i]);

            host.addTab
            (
                host.newTabSpec(days[i]).
                setIndicator(days[i]).
                setContent(contents.getId())
            );

            host.getTabWidget().getChildAt(i).getLayoutParams().height = 55;
        }
    }
}

我的布局文件如下所示:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="6dip">

    <TextView
        android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Test"
        android:textAppearance="?android:attr/textAppearanceMedium" />

</LinearLayout>

问题在于,不是文本位于单独的选项卡上,而是所有文本都相互重叠,并且对于每个选项卡都是相同的。我可能会忽视它,这可能是非常简单的事情。任何帮助表示赞赏。

2 个答案:

答案 0 :(得分:2)

 all the text overlaps eachother and is the same for every tab

他们与我的发现并没有重叠。如果从布局文件中删除android:text="Test",则只会在所有选项卡中看到星期五。

看起来您需要使用TabContentFactory来实现您想要的效果。这是我的问题解决方案。它会为您提供所需的一切。

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

    final TabHost host = getTabHost();
    Resources res = getResources();

    final String[] days = { "Monday", "Tuesday", "Wednesday", "Thursday",
            "Friday" };

    for (int i = 0; i < days.length; i++) {
        TabHost.TabSpec spec=host.newTabSpec(days[i]);
        spec.setContent(new TabHost.TabContentFactory() {
            public View createTabContent(String tag) {
                View wv = getLayoutInflater().inflate(R.layout.test, host.getTabContentView(), false);
                ((TextView) wv.findViewById(R.id.title)).setText(tag);
                return(wv);
            }
        });
        spec.setIndicator(days[i]);
        host.addTab(spec);
    }
    host.setCurrentTab(0);
}

以下是我开始使用的链接https://github.com/commonsguy/cw-android/tree/master/Fancy/DynamicTab

归功于Mark Murphy(Commonsguy)。

答案 1 :(得分:0)

很有趣,但是当我为每个标签传递唯一文字时,我的问题就解决了(即天[i] ,而不是常数“星期五”):)

TabHost.TabSpec spec=host.newTabSpec(days[i]);