0

main.xml中布局只包含两个按钮内容区域,这下图为:如何在我的情况按下按钮时更新内容? (翼状特征)

<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="fill_parent" 
     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)。所以我准备了两个其他内容布局:

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> 

用我的简单代码上面显示,我想实现以下功能:

in main.x毫升

  • 被按下时button_onecontent_one.xml将被包埋到content_area的main.xml中;

  • 当按下button_twomain.xml中content_area将被更新以content_two.xml

使用按钮以创建薄片形特征这意味着。

我的问题是如何更新已经内嵌我main.xml中布局content_area内外部布局的xml文件( & content_two.xml例如content_one.xml)的content_area?

这就是:

button_one.setOnClickListener(new OnClickListener(){ 
    @Override 
    public void onClick(View v){ 
     //What to do here?? to update the content_area in main.xml with an external xml layout 
    } 
}); 

---------------- UPDATE ------------------- ---------

我想:

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

但它不工作,为什么?

+0

此之前的回答可以帮助你:http://stackoverflow.com/questions/5961522/help-how-can-i-do-includein-xml-programmatically-in-my-app-android – Fortega

+0

@ Fortega,我试过但是不行,看我的更新 – Leem

回答

0

您可以使用LayoutInflater从xml文件创建视图层次结构。以下代码返回我的xml视图。

LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
View inflatedView = inflater.inflate(R.layout.content_one, null); 

膨胀(...)的第一个参数是要膨胀的xml文件。第二个参数是充气视图的可选父项。就我而言,我没有一个。然后,您可以将视图添加到您的内容区域。

contentArea.addView(inflatedView); 
+0

@ spidy,我试过你的方法,它不起作用,外部布局根本没有显示 – Leem

+0

@ spidy,看到我在帖子中的更新 – Leem

+0

它的工作原理是,如果没有内容,则大小可能为0,或者视图需要失效并重新绘制。 – Spidy

相关问题