如何在android中使用View Stub

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11577777/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 08:06:52  来源:igfitidea点击:

How to use View Stub in android

androidviewstub

提问by posteritysystem

I want to use ViewStub in android, so please help me. I have created

我想在android中使用ViewStub,所以请帮助我。我创造了

ViewStub stub = new ViewStub;
View inflated = stub.inflate(); 

How to use it programmatically?

如何以编程方式使用它?

回答by mneri

Like the documentationsays, ViewStubis a Viewthat is inflated lazily.

就像文档说的那样,ViewStub是一个View懒惰地膨胀的。

You can declare a ViewStubin an XML file like this:

您可以ViewStub像这样在 XML 文件中声明 a :

 <ViewStub android:id="@+id/stub"
           android:inflatedId="@+id/subTree"
           android:layout="@layout/mySubTree"
           android:layout_width="120dip"
           android:layout_height="40dip" />

The android:layoutattribute is a reference to the Viewthat will be inflated next to a call of inflate(). So

android:layout属性是对 的引用,该引用View将在 的调用旁边膨胀inflate()。所以

ViewStub stub = (ViewStub) findViewById(R.id.stub);
View inflated = stub.inflate();

When the method inflate()is invoked the ViewStubis removed from its parent and replaced with the right View(the root view of mySubTreelayout).

inflate()调用该方法时,ViewStub从其父级中移除并替换为右侧ViewmySubTree布局的根视图)。

If you want to do this progammatically then your code should be something like:

如果您想以编程方式执行此操作,那么您的代码应该类似于:

ViewStub stub = new ViewStub(this);
stub.setLayoutResource(R.layout.mySubTree);
stub.inflate(); 

回答by huseyin

Simply a ViewStub is used to increase efficiency of rendering layout. By using ViewStub, manually views can be created but not added to view hierarchy. At the runtime, can be easily inflated, while ViewStub is inflated, the content of the viewstub will be replaced the defined layout in the viewstub.

简单地使用一个 ViewStub 来提高渲染布局的效率。通过使用 ViewStub,可以手动创建视图,但不能将其添加到视图层次结构中。在运行时,可以很容易地膨胀,当 ViewStub 膨胀时,viewstub 的内容将被 viewtub 中定义的布局替换。

activity_main.xml we defined viewstub but not created first.enter image description here

activity_main.xml 我们定义了viewstub 但不是先创建的。在此处输入图片说明

Simple example gives better understanding,

简单的例子可以更好地理解,

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/content"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
    <Button
        android:id="@+id/btn1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="create the view stub" />
     <Button
        android:id="@+id/btn2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Hide the stub." />

    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="1" >

        <ViewStub
            android:id="@+id/stub_import"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:inflatedId="@+id/content_import"
            android:layout="@layout/splash" />
    </RelativeLayout>
</LinearLayout>

At runtime, when we inflate, the content will be replaced with the layout defined in the viewstub.

在运行时,当我们膨胀时,内容将替换为视图存根中定义的布局。

public class MainActivity extends Activity {

    Button b1 = null;
    Button b2 = null;

    ViewStub stub = null;
    TextView tx = null;

    int counter = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        b1 = (Button) findViewById(R.id.btn1);
        b2 = (Button) findViewById(R.id.btn2);
        b1.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (stub == null) {     
                    stub = (ViewStub) findViewById(R.id.stub_import);
                    View inflated = stub.inflate();
                    tx = (TextView) inflated.findViewById(R.id.text1);
                    tx.setText("thanks a lot my friend..");
                } 

            }
        });

        b2.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                if (stub != null) {
                    stub.setVisibility(View.GONE);
                }

            }
        });

    }

enter image description here

在此处输入图片说明

So, Lets look at again the view hierarchy,

所以,让我们再看看视图层次结构,

enter image description here

在此处输入图片说明

when we inflate the viewstub, it will be removed from the view hierarchy.

当我们膨胀视图存根时,它将从视图层次结构中删除。

回答by Phan Van Linh

Here is an example for show/hide and change data of ViewStubat run time

这是ViewStub运行时显示/隐藏和更改数据的示例

activity_main.xml

活动_main.xml

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

    <Button
        android:id="@+id/buttonShow"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Show View Stub"/>

    <Button
        android:id="@+id/buttonHide"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hide View Stub"/>

    <ViewStub
        android:id="@+id/viewStub"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout="@layout/layout_of_view_stub"
        />

</LinearLayout>

layout_of_view_stub.xml

layout_of_view_stub.xml

<TextView
    android:id="@+id/textInViewStub"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="ViewStub Button"
    />

MainActivity.java

主活动.java

public class MainActivity extends AppCompatActivity {
    private ViewStub viewStub;
    private Button buttonShow;
    private Button buttonHide;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        buttonShow = findViewById(R.id.buttonShow);
        buttonHide = findViewById(R.id.buttonHide);
        buttonShow.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showViewStub();
            }
        });

        buttonHide.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                hideViewStub();
            }
        });
    }

    private void showViewStub() {
        if (viewStub == null) {
            viewStub = findViewById(R.id.viewStub);

            // If you want to change data of ViewStub at runtime, you can do like this
            View inflatedView = viewStub.inflate();
            TextView textViewInViewStub = inflatedView.findViewById(R.id.textInViewStub);
            textViewInViewStub.setText("ABC");
        }
        viewStub.setVisibility(View.VISIBLE);
    }

    private void hideViewStub() {
        if (viewStub == null) {
            return;
        }
        viewStub.setVisibility(View.GONE);
    }
}