1

これは、数日間私を悩ませてきた問題です。私は解決策を探すために最善を尽くしましたが、どこに行っても、現在使用しているアプローチが正しいと言っています。

状況は単純です。5つのタブがあり、コンテンツがわずかに異なる同じレイアウトが含まれています(たとえば、setTextを使用してラベルの1つを変更します)。そのため、レイアウトを5回膨らませ、すべてを個別のタブとして追加しています。

これは私のコードです:

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>

問題は、テキストが別々のタブにあるのではなく、すべてのテキストが互いに重なり合っており、すべてのタブで同じであるということです。それは私が見落としている本当に単純なものかもしれません。どんな助けでも大歓迎です。

4

2 に答える 2

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) にあります。

于 2011-10-23T13:56:37.913 に答える
0

楽しいですが、各タブに一意のテキストを渡すと、問題は解決しました(つまり、一定の「金曜日」の代わりにdays[i] :)

TabHost.TabSpec spec=host.newTabSpec(days[i]);
于 2012-05-23T10:42:48.040 に答える