0
点赞
收藏
分享

微信扫一扫

android三种布局优化的方法:include,merge,ViewStub


常用的android布局优化方法有三种:include,merge,ViewStub,下面我们来说一说他们的使用方法和使用场合:

1.include方法:

用<include />标签来引用一个布局文件,例如,有一个头布局文件如下:

head.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="wrap_content"
android:orientation="vertical" >

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="1111"/>

</LinearLayout>

如果需要引用这个布局文件,那么就使用如下方法:

<include layout="@layout/head"/>

include的使用场合:用于一些可以重用的界面布局,比如说标题栏,这里我引用了QQ的界面如下:

android三种布局优化的方法:include,merge,ViewStub_android开发

这个界面最上方的聊天记录标题栏就可以用include设置,因为可以重用。

2.merge方法:

使用merge标签来布局,可以将merge标签里面的空间都组合起来,但是这个标签使用有一个限制条件:1.布局根节点一定要是FrameLayout并且不需要设置多余的属性,只需要有layout_weight和layout_width即可。

使用方法如下:

如下mergelayout.xml文件,代码如下:


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

<ProgressBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"/>

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="wait"
android:layout_gravity="center"/>


</merge>


引用上面布局文件的方法如下(这里的FrameLayout一定不要有多余的属性):


<FrameLayout 
android:layout_width="match_parent"
android:layout_height="wrap_content">

<include layout="@layout/mergelayout"/>

</FrameLayout>

这时我们看到这两个组件组合起来的效果:

android三种布局优化的方法:include,merge,ViewStub_android开发_02


3.ViewStub方法:

这个方法又叫做惰性加载的方法,对于某些不常用的组件,如果想动态显示的话,有两种方法:第一种是用setVisibility,第二种就是用ViewStub,ViewStub只有你调用了inflate方法,它才会加载界面。用法如下:

首先用布局包装不常用的组件:

stublayout.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="wrap_content"
android:orientation="vertical" >

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="stub view"/>

</LinearLayout>

在主布局中设置一个按钮和ViewStub,ViewStub引用

stublayout.xml布局文件:

<Button 
android:id="@+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="显示隐藏按钮"/>

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


对于显示ViewStub引用的组件,调用inflate即可,但是

ViewStub只能Inflate一次,之后ViewStub对象会被置为空。按句话说,某个被ViewStub指定的布局被Inflate后,就不会够再通过ViewStub来控制它了。这是我们如果需要隐藏和显示文件,那么久只能用setVisibility来设置。代码如下:

Button btn;
ViewStub viewStub;
private boolean flag1= true;
private boolean flag2 = true;

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

btn = (Button)findViewById(R.id.btn);
viewStub = (ViewStub)findViewById(R.id.viewStub);

btn.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (flag1) {
viewStub.inflate();
flag1 = false;
} else {
if (flag2) {
viewStub.setVisibility(View.GONE);
flag2 = false;
} else {
viewStub.setVisibility(View.VISIBLE);
flag2 = true;
}
}
}
});
}


ViewStub的应用场景,就继续用QQ来说吧,下图中的

android三种布局优化的方法:include,merge,ViewStub_android开发_03

本来“附近的群”下面有几个部落之类的组件的,但是可以设置他不显示。

举报

相关推荐

0 条评论