< include>标签的使用
用法实例:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.shinelon.maptestactivity.Main2Activity">
<include layout="@layout/reuse" />
</LinearLayout>
用法解说:
@layout/reuse:就是我们所引用过来的复用布局。
这里注意的是,当要给 <include layout="@layout/reuse" />里面添加属性时,
必须把android:layout_width 和android:layout_height写上,否则不起作用(如果
这里写id属性就这里为准,并且其他属性必须是android:layout_ 开头的属性)
例如:
<include
android:id="@+id/ic_reuse"
layout="@layout/reuse"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<merge>标签的使用
解说:
上面布局是用LinearLayout的垂直布局,如果里面的布局也就是@layout/reuse这个布局
也采用了竖直方向的LinearLayout,则里面就可以直接用<merge>标签从而来减少布局的层级。
例如修改后(里面的布局@layout/reuse):
<merge xmlns:android="http://schemas.android.com/apk/res/android">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="公用布局1"
android:textSize="36sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="公用布局2"
android:textSize="36sp" />
</merge>
ViewStub 的使用
解说:
ViewStub继承了View,是一个轻量级且宽高都为0,因此本身不参与任何布局和绘制过程。
ViewStub的意义就是按需加载所需要的界面,在实际开发中,有很多布局文件在正常情况下也要隐藏,
比如网络异常,就没必要去初始化时加载进来了,通过他就可以在界面使用的时候在加载,提高初始化
的性能。
例如:
android:id="@+id/vs_id"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:inflatedId="@+id/root_id"
android:layout="@layout/root" />
解说:
id 自然就是ViewStub的id了。
inflatedId是@layout/root里的id
使用:
在使用这个ViewStub时,有两种方式:
1、 ((ViewStub) findViewById(R.id.vs_id)).setVisibility(View.VISIBLE);
2、 View vs = ((ViewStub) findViewById(R.id.vs_id)).inflate();
当用上面两种方法加载后,ViewStub就会被内部的布局@layout/root替换掉,这个时候ViewStub就不再是布局中的一部分了。
另外,ViewStub暂时不支持<merge>标签。
网友评论