ViewStub的小例子
ViewStub是一个很有趣的小控件,通过它我们可以动态控制程序显示哪个布局。ViewStub是看不见的,不会占用布局位置。当ViewStub通过.inflate()调用时,ViewStub所指向的布局会被实例化,ViewStub的属性也会传给所指向的布局。下边是一个小例子,实现两个按钮,通过ViewStub来实现文本和图像的载入。
MainActivity
package com.bupt.markfavor.demo0915viewstub;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewStub;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private Button button1;
private Button button2;
private ViewStub viewStub_text;
private ViewStub viewStub_image;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findviews();
setListeners();
}
private void findviews() {
button1 = (Button) findViewById(R.id.showtext);
button2 = (Button) findViewById(R.id.showimage);
viewStub_text = (ViewStub) findViewById(R.id.viewstub_text);
viewStub_image = (ViewStub) findViewById(R.id.viewstub_image);
}
private void setListeners() {
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
viewStub_text.inflate();
TextView text = (TextView) findViewById(R.id.viewstub_textview);
text.setText("Test viewstub");
}
});
button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
viewStub_image.inflate();
ImageView image = (ImageView) findViewById(R.id.stubview_imageview);
}
});
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:gravity="center_horizontal"
android:orientation="vertical"
tools:context="com.bupt.markfavor.demo0915viewstub.MainActivity">
<Button
android:id="@+id/showtext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="show text" />
<Button
android:id="@+id/showimage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="show image" />
<ViewStub
android:id="@+id/viewstub_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout="@layout/viewstub_text_layout"/>
<ViewStub
android:id="@+id/viewstub_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout="@layout/viewstub_image_layout"/>
</LinearLayout>
viewstub_image_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:gravity="center_horizontal"
android:orientation="vertical"
tools:context="com.bupt.markfavor.demo0915viewstub.MainActivity">
<Button
android:id="@+id/showtext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="show text" />
<Button
android:id="@+id/showimage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="show image" />
<ViewStub
android:id="@+id/viewstub_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout="@layout/viewstub_text_layout"/>
<ViewStub
android:id="@+id/viewstub_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout="@layout/viewstub_image_layout"/>
</LinearLayout>
viewstub_text_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/viewstub_textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
在MainActivity里,定义了两个按钮和它们的点击事件以及viewstub控件,点击事件里对viewstub控件进行了渲染(.inflate()不知道这么翻译对不对)。布局文件也没有什么好说的,就是额外定义了两个布局用来装载textview和imageview。通过viewstub来指定。