2011-10-23 22 views
1

これは数日間私を困らせている問題です。私は解決策を探し求めましたが、どこに行くにしても、私が現在使っているアプローチが正しいと言います。タブに同じレイアウトの複数のインスタンスを膨張させる

状況はシンプルです:私は5つのタブを持っていますが、これはわずかに異なる内容の同じレイアウトを含んでいます(たとえば、ラベルの1つを変更するためにsetTextを使用するなど)。そのように、私はレイアウトを5回膨らませ、すべてを別々のタブとして追加しています。

これは私のコードです:問題ではなく、テキストは別々のタブにあることの、すべてのテキストがお互いに重なって、ということである

<?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> 

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

そして、私のレイアウトファイルは次のようになりますすべてのタブで同じです。私が見落としているのは本当にシンプルなものかもしれません。どんな助けもありがとうございます。

答えて

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

クレジットはマーク・マーフィー(Commonsguy)になり始めるのリンクです。

+0

おかげで、[i]はすなわち日(楽しいですが、私は、各タブの一意のテキストを通過したとき、私の問題が解決されました!それが私の問題を解決しました。しかし私は不思議に思っています...インフレが毎回ビューの新しいインスタンスを作成するという前提で間違っていますか? – Sven

+1

一般的に膨らませると新しいビューが作成されます。しかし、これは 'inflate(R.layout.test、host.getTabContentView()、true)'ではありません。それはあなたが膨張させるたびに同じビューを返すでしょう。 – PH7

+0

十分に公正。ありがとう。 – Sven

0

ではなく一定の「金曜日」:)

TabHost.TabSpec spec=host.newTabSpec(days[i]); 
関連する問題