2012-03-24 24 views
1

私は、タブレイアウトを使用するアクティビティを持つAndroidアプリケーションで作業しています。いくつかのTextViewに表示される内容を切り替える2つのタブがあります。TabHost内の2つの異なるタブに同じレイアウトを表示するにはどうすればいいですか?

これは、二つのタブ仕様は、コンテンツのために同じレイアウト(リニア)にR.id.plantillaを指していることを意味します

<?xml version="1.0" encoding="utf-8"?> 
<TabHost xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@android:id/tabhost" 
android:layout_width="match_parent" 
android:layout_height="match_parent" > 

<LinearLayout 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" > 

    <TabWidget 
     android:id="@android:id/tabs" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" /> 

    <FrameLayout 
     android:id="@android:id/tabcontent" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" > 

     <include layout="@layout/plantilla"/> 


    </FrameLayout> 
</LinearLayout> 

しかし、これだけ作品、私はタブ2に切り替えるとし、 1に戻ります。つまり、アクティビティが開始されると、タブが変更される前にLayout "plantilla"が表示されません。これは私の問題です。

これを回避する最も簡単な方法は何ですか?

PD:私はラインをtabhost XML内

<include layout="@layout/plantilla"> 

を複製しようとしているが、この場合、IはfindViewById(R.id.someTextView)を使用してJavaコードからTextViewsオブジェクトにアクセスすることはできません;

答えて

0

インクルードは、あなたのxmlレイアウトからタグを含める削除し、シンプルなものを残してください:

<?xml version="1.0" encoding="utf-8"?> 
<!-- Tab Host --> 
<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"> 

    <!-- Bottom tab bar --> 
    <LinearLayout 
     android:orientation="vertical" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:padding="5dp"> 

     <FrameLayout 
      android:id="@android:id/tabcontent" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:padding="5dp" 
      android:layout_weight="1"/> 

     <!-- -5dip bottom margin for not showing the annoying bottom line --> 
     <TabWidget 
      android:id="@android:id/tabs" 
      android:layout_width="fill_parent" 
      android:layout_height="65dip" 
      android:layout_weight="0" 
      android:layout_marginBottom="-5dip"/> 

    </LinearLayout> 

</TabHost> 

そして、あなたのTabActivityクラスからわずかのonCreate(でこれを置く):

  //Set tab host 
     tabHost = getTabHost(); 

     //First Tab 
     TabSpec firstTabSpec = tabHost.newTabSpec("FirstTab");//title 
     firstTabSpec.setIndicator("FirstTab", getResources().getDrawable(R.drawable.item_first_tab)); //icon 
     Intent firstTabIntent = new Intent(this, FirstTab.class);//action 
     firstTabSpec.setContent(firstTabIntent);//set tab 

     //Second Tab 
     TabSpec secondTabSpec = tabHost.newTabSpec("SecondTab");//title 
     secondTabSpec.setIndicator("SecondTab", getResources().getDrawable(R.drawable.second_tab));//icon 
     Intent secondTabIntent = new Intent(this, FirstTab.class);//action 
     secondTabSpec.setContent(secondTabIntent);//set tab 

     //Add tabs to tab host 
     tabHost.addTab(firstTabSpec);//add first tab 
     tabHost.addTab(secondTabSpec);//add second tab which goes to your FirstTab.class 

あなたは別の活動を作成することができますあなたのタブのために、または1つを使用します。

+0

ありがとう、友人ですが、この方法は私がタブでアクティビティを持ちたくないので私を助けません。 TabActivityという1つのアクティビティが必要です。マークされたタブに応じて異なるコンテンツが表示されます。各タブに表示される内容は、TabActivityを持つプライベートオブジェクトから読み取られます。私が複数のアクティビティを持っている場合、私はこのオブジェクトを2つのタブのレイアウトと同じよりも複雑なアクティビティで共有する必要があります。 – cs4r

1

私はあなたがincludeを使うことができるとは思っていませんが、実際には異なるidタグを使ってxmlでレイアウトを2回定義する必要があると思います。

単純にコンテナビューを定義してから、プログラムでビューを追加することもできます。私はそれを前にやった。ここで私はそれをやった方法は次のとおりです。続き

tablayout

<?xml version="1.0" encoding="utf-8"?> 
<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" > 
    <LinearLayout 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:orientation="vertical" 
      android:padding="5dp" > 
      <TextView 
        android:id="@+id/setupheader" 
        android:layout_width="fill_parent" 
        android:layout_height="20dp" 
        android:textSize="15dp" /> 
      <TabWidget 
        android:id="@android:id/tabs" 
        android:layout_width="fill_parent" 
        android:layout_height="30dp" 
        android:gravity="bottom" /> 
      <FrameLayout 
        android:id="@android:id/tabcontent" 
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent" 
        android:padding="5dp" > 
        <!-- General Info Tab --> 
        <LinearLayout 
          android:id="@+id/general_tab" 
          android:layout_width="fill_parent" 
          android:layout_height="wrap_content" 
          android:orientation="vertical" > 
        </LinearLayout> 
        <!-- Tool Tab --> 
        <LinearLayout 
          android:id="@+id/tool_tab" 
          android:layout_width="fill_parent" 
          android:layout_height="wrap_content" 
          android:orientation="vertical" > 
          <ListView 
            android:id="@+id/list1" 
            android:layout_width="match_parent" 
            android:layout_height="0dp" 
            android:layout_weight="1" 
            android:drawSelectorOnTop="false" /> 
        </LinearLayout> 
        <!-- Offset Tab --> 
        <LinearLayout 
          android:id="@+id/offset_tab" 
          android:layout_width="fill_parent" 
          android:layout_height="wrap_content" 
          android:orientation="vertical" > 
          <ListView 
            android:id="@+id/list2" 
            android:layout_width="match_parent" 
            android:layout_height="0dp" 
            android:layout_weight="1" 
            android:drawSelectorOnTop="false" /> 
        </LinearLayout> 
        <!-- Notes Tab --> 
        <LinearLayout 
          android:id="@+id/note_tab" 
          android:layout_width="fill_parent" 
          android:layout_height="fill_parent" 
          android:orientation="vertical" > 
        </LinearLayout> 
      </FrameLayout> 
    </LinearLayout> 
</TabHost> 

は(私はかなりの部分を削除した私のタブの活動ですが、それは、私はそれぞれに別々のactivitesずにタブをやって行きましたか十分に示さなければなりませんタブ)。

tabactivity

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

      Bundle extras = getIntent().getExtras(); 
      RowId = extras.getLong("RowId"); 
      StartTab = extras.getInt("StartTab"); 

      // *************************************************************** 
      // Set up the tabs in the tabhost 
      // *************************************************************** 
      tabHost = getTabHost(); 
      TabHost.TabSpec spec; 
      spec = tabHost.newTabSpec("General").setIndicator("General") 
          .setContent(R.id.general_tab); 
      tabHost.addTab(spec); 
      spec = tabHost.newTabSpec("Tools").setIndicator("Tools") 
          .setContent(R.id.tool_tab); 
      tabHost.addTab(spec); 
      spec = tabHost.newTabSpec("Offsets").setIndicator("Offsets") 
          .setContent(R.id.offset_tab); 
      tabHost.addTab(spec); 
      spec = tabHost.newTabSpec("Notes").setIndicator("Notes") 
          .setContent(R.id.note_tab); 
      tabHost.addTab(spec); 
      populateTabs(StartTab); 
    } 

    // *************************************************************** 
    // Clear views from linear layout tabs 
    // *************************************************************** 
    private void clearTabs() { 
      general.removeAllViews(); 
      notes.removeAllViews(); 
    } 

    // *************************************************************** 
    // Fill the tabs 
    // *************************************************************** 
    private void populateTabs(int TabShown) { 

      generaltab(); 
      tooltab(); 
      notestab(); 
      offsettab(); 

      tabHost.setCurrentTab(TabShown); 
    } 

    // *************************************************************** 
    // Fill the General tab 
    // *************************************************************** 
    private void generaltab() { 
     general = (LinearLayout) findViewById(R.id.general_tab); 
     String prgdisp = SETUPINFO_PGM1; 
     if (SETUPINFO_PGM2 != null) { 
      prgdisp = prgdisp + ", " + SETUPINFO_PGM2; 
     } 
     if (SETUPINFO_PGM3 != null) { 
      prgdisp = prgdisp + ", " + SETUPINFO_PGM3; 
     } 
     TextView programs = new TextView(this); 
     programs.setText("Program(s): " + prgdisp); 
     programs.setTextSize(20); 
     general.addView(programs); 

     if (SETUPINFO_KIT == null || SETUPINFO_KIT.equals("No Kit")) { 
     } else { 
      TextView kit = new TextView(this); 
      kit.setText("Kit: " + SETUPINFO_KIT); 
      kit.setTextSize(20); 
      general.addView(kit); 
     } 

     if (SETUPINFO_FIXTURE1 == null && SETUPINFO_FIXTURE2 == null) { 
     } else { 
      String fixtdisp = SETUPINFO_FIXTURE1; 
      if (SETUPINFO_FIXTURE2 != null) { 
       fixtdisp = fixtdisp + ", " + SETUPINFO_FIXTURE2; 
      } 
      TextView fixtures = new TextView(this); 
      fixtures.setText("Fixture(s): " + fixtdisp); 
      fixtures.setTextSize(20); 
      general.addView(fixtures); 
      TextView fixtureloc = new TextView(this); 
      fixtureloc.setText("Fixture Location: " + SETUPINFO_FIXTURELOC); 
      fixtureloc.setTextSize(20); 
      general.addView(fixtureloc); 
     } 

     if (SETUPINFO_VISE == null) { 
     } else { 
      TextView vise = new TextView(this); 
      vise.setText("Vise(s): " + SETUPINFO_VISE); 
      vise.setTextSize(20); 
      general.addView(vise); 
     } 
    } 

    // *************************************************************** 
    // Fill the Offset tab 
    // *************************************************************** 
    private void offsettab() { 
      wpccount = 0; 
      for (int i = 0; i < 20; i++) { 
        if (wpcdesc[i] != null) { 
          wpcdisplayhold[wpccount] = wpcid[i] + " - " + wpcdesc[i]; 
          wpcidhold[wpccount] = wpcid[i]; 
          wpcdeschold[wpccount] = wpcdesc[i]; 
          wpccount++; 
        } 
      } 
      wpcdisplay = new String[wpccount]; 
      for (int i = 0; i < wpccount; i++) { 
        wpcdisplay[i] = wpcdisplayhold[i]; 
      } 
      mWPCView = (ListView) findViewById(R.id.list2); 
      mWPCView.setAdapter(new ColorizingListAdapter(SetupDisplay.this, 
          wpcdisplay, "Offset")); 
      registerForContextMenu(mWPCView); 
    } 

    // *************************************************************** 
    // Fill the Notes tab 
    // *************************************************************** 
    private void notestab() { 
      notes = (LinearLayout) findViewById(R.id.note_tab); 
      notestxt = new TextView(this); 
      notestxt.setText(SETUPINFO_NOTES); 
      notestxt.setTextSize(15); 
      notes.addView(notestxt); 
    } 

} 

は、この情報がお役に立てば幸いです。

関連する問題