个性化阅读
专注于IT技术分析

android viewstub用法

ViewStub是大小为零的不可见视图,用于在运行时加载“布局资源”。 ViewStub是零尺寸视图,因此你不会在布局面板上看到任何内容。

为了使父资源可见,调用inflate()方法。为了使ViewStub可见或不可见,将调用setVisibility(int)方法。 View.VISIBLE常量用于使ViewStub可见,而View.GONE常量用于不可见。

ViewStub示例

让我们创建一个ViewStub View的示例,该示例显示和隐藏在另一个布局(my_layout.xml)文件中创建的ImageView(图像)。

文件:activity.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.test.viewstubexample.MainActivity">

    <ViewStub
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/viewStub"
        android:layout_marginLeft="120dp"
        android:layout="@layout/my_layout"
        android:layout_alignParentTop="true" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Show"
        android:id="@+id/show"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginLeft="65dp"
        android:layout_marginStart="65dp" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hide"
        android:id="@+id/hide"
        android:layout_alignParentBottom="true"
        android:layout_toRightOf="@+id/show"
        android:layout_toEndOf="@+id/show"
        android:layout_marginLeft="51dp"
        android:layout_marginStart="51dp" />

</RelativeLayout>

档案:my_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/imageView"
        android:background="@drawable/image"
        />

</LinearLayout>

文件:MainActivity.java

package com.example.test.viewstubexample;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewStub;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {
    ViewStub viewStub;
    Button show, hide;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        show=(Button)findViewById(R.id.show);
        hide=(Button)findViewById(R.id.hide);
        viewStub=(ViewStub)findViewById(R.id.viewStub);
        viewStub.inflate();

        show.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                viewStub.setVisibility(View.VISIBLE);
            }
        });
        hide.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                viewStub.setVisibility(View.GONE);
            }
        });
    }
}

输出量

赞(0)
未经允许不得转载:srcmini » android viewstub用法

评论 抢沙发

评论前必须登录!