1

マイmain.xmlレイアウトは単に二つのボタン、以下を示しコンテンツエリアを、含まれています私の場合、LayoutInflaterを使用して古いコンテンツを削除して新しいコンテンツを表示するにはどうすればよいですか?

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical"> 


    <LinearLayout 
     android:id="@+id/myBtns" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:orientation="horizontal" 
    > 
     <Button 
      android:id="@+id/btn_one" 
      android:layout_width="100dip" 
      android:layout_height="30dip" 
       android:text="button one" 
     /> 
     <Button 
      android:id="@+id/btn_two" 
      android:layout_width="100dip" 
      android:layout_height="30dip" 
       android:text="button two" 
     /> 
    </LinearLayout> 

    <LinearLayout 
     android:id="@+id/content_area" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
    > 
     <!--different button press will embed different content here--> 

    </LinearLayout> 


    </LinearLayout> 

私は各ボタンを押して自分のタブ状機能を作成したいですボタンの下のコンテンツ(content_area)を更新します。だから、私は2つの他のコンテンツのレイアウトを用意しています

content_one.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical"> 

    <TextView.../> 
    <Button .../> 

</LinearLayout> 

がcontent_two.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical"> 

    <Gallery.../> 
    <Button .../> 

</LinearLayout> 

をすべての私の単純なコードでは、私は希望、上記示しました。次のような機能を実装する:

main.x mlの

  • button_oneが押されたときに、content_one.xmlcontent_areaのmain.xmlに埋め込まれます。 button_twoが押されたときに

  • は、main.xmlcontent_areaタブ状機能を作成するには、ボタンを使用することを意味content_two.xml

に更新されます。

は、私が試した:上記のコードは、部分的に作業

button_one.setOnClickListener(new OnClickListener(){ 
     @Override 
     public void onClick(View v){ 
      LayoutInflater inflater = (LayoutInflater)MyActivity.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      View inflatedView = inflater.inflate(R.layout.content_one, null); 

      LinearLayout contentArea= (LinearLayout) findViewById(R.id.content_area); 
      contentArea.addView(inflatedView); 
     } 
    }); 

button_two.setOnClickListener(new OnClickListener(){ 
      @Override 
      public void onClick(View v){ 
       LayoutInflater inflater = (LayoutInflater)MyActivity.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
       View inflatedView = inflater.inflate(R.layout.content_two, null); 

       LinearLayout contentArea= (LinearLayout) findViewById(R.id.content_area); 
       contentArea.addView(inflatedView); 
      } 
     }); 

、私は何を意味するbutton_oneが押されたときbutton_twoが押されたときに、content_areacontent_one.xmlまたはを示すことです、 content_areaにはcontent_two.xmlが表示されています。

しかし、いずれかのボタンを最初に押した後、もう一方のボタンを押すと、コンテンツは古いボタンでトリガされたコンテンツに残ります。私はbutton_twoを押すと、例えば、アプリケーションが最初にロードされ、まずbutton_oneを押すと、content_one.xmlが表示され、その後に、コンテンツは、はcontent_one.xml残っています。

私の質問はLayoutInflaterを使用したときに、古いコンテンツを削除し、新しいコンテンツを表示する方法ですか?

答えて

2

問題は、新しいビューを追加するときに古いビューを削除していないことです。

contentArea.removeAllViews()

編集:注ビューにボタンが押されるたびに膨張させ、あなたの以前の状態が常に失われることは、新しいビューを追加する前にこのコード行を貼り付けます。あなたがビューの状態を維持する必要があるなら、私はIdisticの提案に行きます。どのような他の人が行っている

関連する問題