Android ViewStub的基本使用
来源:互联网 发布:免费x站 最新域名网站 编辑:程序博客网 时间:2024/05/20 07:35
ViewStub可以在运行时动态的添加布局。帮助文档给定的定义是:
1
A ViewStub is an invisible, zero-sized View that can be used to lazily inflate layout resources at runtime. When a ViewStub is made visible, or when inflate() is invoked, the layout resource is inflated. The ViewStub then replaces itself in its parent with the inflated View or Views. Therefore, the ViewStub exists in the view hierarchy until setVisibility(int) or inflate() is invoked. The inflated View is added to the ViewStub's parent with the ViewStub's layout parameters. Similarly, you can define/override the inflate View's id by using the ViewStub's inflatedId property.
总之是:ViewStub可以给其他的view事先占据好位置,当需要的时候调用inflater()或者是setVisible()方法显示这些View组件。
layout.xml:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
<
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:orientation
=
"vertical"
tools:context
=
".MainActivity"
>
<
LinearLayout
android:layout_width
=
"match_parent"
android:layout_height
=
"wrap_content"
android:orientation
=
"horizontal"
>
<
Button
android:id
=
"@+id/showBtn"
android:layout_width
=
"match_parent"
android:layout_height
=
"50dip"
android:layout_weight
=
"1"
android:text
=
"show"
/>
<
Button
android:id
=
"@+id/deleteBtn"
android:layout_width
=
"match_parent"
android:layout_height
=
"50dip"
android:layout_weight
=
"1"
android:text
=
"delete"
/>
</
LinearLayout
>
<
LinearLayout
android:layout_width
=
"wrap_content"
android:layout_height
=
"wrap_content"
>
<
ViewStub
android:id
=
"@+id/viewstub"
android:layout_width
=
"wrap_content"
android:layout_height
=
"wrap_content"
android:layout
=
"@layout/next"
/>
</
LinearLayout
>
</
LinearLayout
>
next.xml:
1
2
3
4
5
6
7
8
9
10
11
12
13
<?
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"
android:orientation
=
"vertical"
>
<
ProgressBar
android:id
=
"@+id/progressBar1"
style
=
"?android:attr/progressBarStyleLarge"
android:layout_width
=
"wrap_content"
android:layout_height
=
"wrap_content"
/>
</
LinearLayout
>
Main.java:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package
com.example.android_viewstub1;
import
android.os.Bundle;
import
android.annotation.SuppressLint;
import
android.app.Activity;
import
android.view.Menu;
import
android.view.View;
import
android.view.View.OnClickListener;
import
android.view.ViewStub;
import
android.widget.Button;
public
class
MainActivity
extends
Activity {
Button btn1, btn2;
ViewStub viewStub;
@Override
protected
void
onCreate(Bundle savedInstanceState) {
super
.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn1 = (Button)
this
.findViewById(R.id.showBtn);
btn2 = (Button)
this
.findViewById(R.id.deleteBtn);
viewStub = (ViewStub)
this
.findViewById(R.id.viewstub);
btn1.setOnClickListener(
new
OnClickListener() {
@SuppressLint
(
"NewApi"
)
@Override
public
void
onClick(View arg0) {
viewStub.setVisibility(View.VISIBLE);
}
});
btn2.setOnClickListener(
new
OnClickListener() {
@SuppressLint
(
"NewApi"
)
@Override
public
void
onClick(View arg0) {
viewStub.setVisibility(View.INVISIBLE);
}
});
}
}
0 0
- Android ViewStub的基本使用
- Android ViewStub基本使用
- Android ViewStub的使用
- android ViewStub的使用
- Android中ViewStub的使用
- android viewstub include的使用
- Android中ViewStub的使用
- Android小记:ViewStub的使用
- 【Android基础知识】ViewStub的使用
- Android控件之ViewStub的使用技巧
- android--性能提升ViewStub的使用
- Android视图优化之Merge,ViewStub基本使用
- Android ViewStub控件使用
- android viewstub使用注意
- Android ViewStub的应用
- Android--ViewStub的应用
- Android ViewStub的用法
- Android ViewStub的应用
- Grunt 自动化部署之css、image、javascript、html压缩Gruntfile.js配置
- 吴超大数据高薪就业班二期和三期(官方培训费11000)
- 【学习手记】U3D实战之DontTouchWhite(三)
- 访问网络图片笔记
- 吴超大数据高薪就业班二期和三期(官方培训费11000)
- Android ViewStub的基本使用
- 吴超大数据高薪就业班二期和三期(官方培训费11000)
- 利用xcconfig来控制Xcode工程
- 上海C++游戏服务器群活动PPT下载
- C#多线程 为多核处理器而生的多线程方法Parallel.For和Parallel.ForEach
- 浅谈C/C++排序函数中cmp()比较函数的写法
- Android开发之清除程序缓存
- ApkTool在Mac上的安装和使用
- 利用POI技术 处理PPT