【问题标题】:How to update the content when button pressed in my case? (a tab-like feature)在我的情况下按下按钮时如何更新内容? (类似标签的功能)
【发布时间】:2011-07-12 13:37:23
【问题描述】:

我的 ma​​in.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>

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

ma​​in.xml 中:

  • 按下button_one时,content_one.xml会嵌入到ma​​in.xmlcontent_area

  • 按下button_two时,ma​​in.xmlcontent_area会更新为content_two.xml

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

我的问题是如何使用嵌入在我的 content_area 中的外部布局 xml 文件(例如 content_one.xmlcontent_two.xml)更新 content_area ma​​in.xml 布局?

那是:

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

但它不起作用,为什么?

【问题讨论】:

标签: android android-layout android-emulator android-widget android-manifest


【解决方案1】:

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

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

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

contentArea.addView(inflatedView);

【讨论】:

  • @spidy ,我试过你的方法,不行,外部布局根本不显示
  • 可以,如果没有内容,大小可能为0,或者视图需要失效重绘。
  • @Spidy,我确实有内容。请在此处查看我的更新帖子:stackoverflow.com/questions/6665988/…
猜你喜欢
  • 2018-08-07
  • 1970-01-01
  • 2011-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-01-05
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多