ViewStub和View有什么区别吗?

4

我是Android开发的新手。

我从https://developer.android.com/training/improving-layouts/loading-ondemand.html了解到ViewStub。

它提到使用ViewStub更加经济实惠,占用内存更少。

以下是我的代码:

MainLayout.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/Layout1"
    android:layout_width="match_parent"
    android:layout_height="fill_parent"
    android:background="@color/blue"
    android:orientation="vertical" >

.....

.....

<Button android:id="@+id/ShowBackground"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"

<Button android:id="@+id/ShowBackground2"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"

    android:layout_alignParentBottom="true"
    android:text="@string/title_close" />

<View
  android:id="@+id/ShowView"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent" 
  android:background="@color/blue" />

<ViewStub
    android:id="@+id/ShowViewStub"
    android:layout="@layout/test"        
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:visibility="gone" />

</RelativeLayout>

test.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/ShowViewLayout"
    android:layout_width="match_parent"
    android:layout_height="fill_parent"
    android:background="@color/blue_dimmer"
    android:orientation="vertical" >

</RelativeLayout>

主活动:

    private ViewStub mViewStub;
    private View mView;
    private Button mBackground1;    
    private Button mBackground2
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.MainLayout);

        mView = (ViewStub) findViewById(R.id.ShowView);
        mViewStub = (ViewStub) findViewById(R.id.ShowViewStub);
        mBackground1 = (Button)this.findViewById(R.id.ShowBackground);

    mBackground1.setOnClickListener(new OnClickListener() {
      @Override
      public void onClick(View v) {
         mView.setVisibility(View.VISIBLE);
       }
    });
   }

    mBackground2 = (Button)this.findViewById(R.id. ShowBackground2);
    mBackground2.setOnClickListener(new OnClickListener() {
      @Override
      public void onClick(View v) {
         mViewStub.setVisibility(View.VISIBLE);
       }
    });
   }

问题:

View和ViewStub在MainActivity中表现良好。

我知道ViewStub适用于很少使用的视图。如果在MainActivity中至少调用1次ViewStub,那么由于test.xml布局被添加到活动中,不应该有更多的内存使用吗?

我所看到的是ViewStub始终可见,除非它被调用View.Gone....

请问有人能解释一下这两者之间的区别吗?

非常感谢您的帮助,

谢谢。

1个回答

0

<include /> 可以将 XML 内容包含在基本的 XML 文件中,就好像整个文件只是一个大文件一样。这是在不同布局之间共享布局部分的好方法。

<ViewStub /> 有点不同,因为它不是直接包含的,只有在实际使用它/需要它时才会加载,即当您将其可见性设置为 VISIBLE(实际可见)或 INVISIBLE(仍然不可见,但其大小不再为 0)时。这是一种很好的优化方式,因为您可以拥有具有大量小视图或标题的复杂布局,并且仍然可以快速加载 Activity。一旦您使用其中一个视图,它将被加载。


谢谢你的回答。我还是不太明白。你说一旦我使用其中一个视图,它就会被加载。回到我提到的问题,如果我使用mView.setVisibility(View.VISIBLE);一次,test.xml布局将被添加到Activity中,这不会增加更多的内存使用吗?因为test.xml已经被添加并且始终在Activity中。谢谢。 - AndroidMan
如果我使用ViewStub在使用相机应用程序拍照时显示闪光白色背景,这样做比使用View更好还是一样好? - AndroidMan

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接