Android页面的布局优化

本文探讨的是Android开发中布局优化,主要有下面的三个原则:

1.尽量使用<include/>标签引用相同的可复用的组件;

2.尽量使用RelativeLayout

3.使用<merge/>标签减少布局的嵌套层数;

4.使用<ViewStub/>标签加载不常用的布局。

下面主要探讨 3,4方法:

1.<merge/>的使用:

< merge />标签的作用是合并UI布局,使用该标签能降低UI布局的嵌套层次。该标签的主要使用场景是在你要include的子页面的根部标签和父页面的根部标签的标签是一样的。如例子

先不用<merge/>的样例的测试结果


使用<merge/>的样例的测试代码

activity_main.xml

<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"
    tools:context=".MainActivity" >

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

</RelativeLayout>
activity_second.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" >

    <TextView
        android:id="@+id/click_me_tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:padding="10dp"
        android:text="点击我" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:padding="10dp"
        android:text="返回" />


</merge>

使用<merge/>的样例的测试结果


这样就减少了一层RelativeLayout。

2.<ViewStub/>标签的使用

官方介绍:

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.

大体意思是:ViewStub是一个不可见的,能在运行期间延迟加载的大小为0View,它直接继承于View。当对一个ViewStub调用inflate()方法或设置它可见时,系统会加载在ViewStub标签中引入的我们自己定义的View,然后填充在父布局当中。也就是说,在对ViewStub调用inflate()方法或设置visible之前,它是不占用布局空间和系统资源的。它的使用场景可以是在我们需要加载并显示一些不常用的View时。

新建一个activity_three.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"
    android:gravity="center"
    android:orientation="vertical" >

    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:text="lbc,HelloWord" />

  

</LinearLayout>
在activity_main.xml中引用

<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"
    tools:context=".MainActivity" >

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

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

</RelativeLayout>

main函数的方法测试:

public class MainActivity extends Activity {

	private TextView textView;
	boolean isShow = true;
	View view;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		textView = (TextView) findViewById(R.id.click_me_tv);

		textView.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (isShow) {
					showView();
				} else {
					colseView();
				}
				isShow = !isShow;
			}
		});

	}

	void showView() {

		if (view != null) {
			view.setVisibility(View.VISIBLE);
			return;
		}
		ViewStub stub = (ViewStub) findViewById(R.id.view_stub);
		view = stub.inflate();
	}

	void colseView() {

		if (view != null) {
			view.setVisibility(View.GONE);
		}
	}
}

第一次加载项目,但并未点击按钮的时候测试的视图



当我点击右侧让他显示后,也在他让消失后的测试视图


可以清晰的看到他第一次加载的时候没有加载three_activity.xml,当点击按钮使用它的时候,加载了three_activity.xml。

到此,就结束了,在此感谢给我提示的但是我忘记姓名的大神。如果有什么疑问或好的建议,可以留言。欢迎大家批评指正。谢谢

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值