如何更新内容时,按下按钮在我的情况下?(类似标签的功能)



我的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>

我想创建我自己的tab-like功能,每个按钮按下将更新按钮下面的内容(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>

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

在<<p> strong> main。xml :
  • button_one被按下时,content_one.xml将嵌入到main.xmlcontent_area中;

  • button_two被按下时,main.xmlcontent_area将被更新为content_two.xml

这意味着使用按钮来创建一个类似于标签的功能

我的问题是如何更新content_area与外部布局xml文件(例如content_one.xml &content_two.xml)嵌入到我的main.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
    }
});

---------------- 更新 ----------------------------

我试着:

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

但是它不工作,为什么?

您可以使用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);

相关内容

最新更新