*【布局优化】android布局原则

原则:
(1)尽量使用LinearLayout和RelativeLayout
(2)在布局层次一样的情况下,建议使用LinearLayout代替RelativeLayout,LinearLayout性能更高
(3)将可复用的组件抽取出来并通过include标签使用。
(4)使用ViewStub标签来加载一些不常用的布局
(5)使用merge标签减少布局的嵌套层次


include
作用:将公用的组件抽取出来单独放到一个xml文件中,然后使用include标签导入共用布局;
效果:提高UI的制作和复用效率,也能保证制作的UI布局更加规整和易维护。

使用:
如:< include layout=”@layout/common_title”>
这样就会将我们自定义的common_title这xml文件中的内容显示在我们的布局中去

无标签设置样式
在Manifest.xml文件里面application标签下的theme设置

android:theme="@android:style/Theme.Light.NoTitleBar"

Layout总结
1. include用法
<include    android:id="@+id/include1"
            layout="@layout/commn_title"    />
注意:
(1)android:layout_centerVertical="true"可以调整中间位置
(2)被include进来的布局组件可以通过findViewById()得到并使用

2. merge(见图)
作用:合并UI布局,降低嵌套层次
(1)被include进来的布局文件可以使用merge标签,这样被include进来的布局是叠加的。
<FrameLayout
    <include layout="@layout/common_progress" />
这个common_progress就可以使用merge标签
<merge
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" 
    android:orientation="vertical" >

    <ProgressBar>

    <TextView>

</merge>
加上merge后这两个组件ProgressBar和TextView会叠加在一起

3. ViewStub惰性加载
作用:和include一样可以用来引入一个外部布局,不同的是,viewStub引入的布局默认不会扩张
,既不会占用显示也不会占用位置,从而在解析layout时节省CPU和内存
(1)用法:
<ViewStub 
android:layout="@layout/common_text" //不能使用layout="",否则会exception:viewstub must have a valid layoutresource
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/viewStub"
(2)可以通过ViewStub类的inflate()方法使其显示出来。
使用merge合并UI布局,merge就相当于帧布局
Viewstub惰性加载:可以控制视图的显示,即不会占用显示也不会占用位置
,从而在解析layout时节省cpu和内存;与Hibernate的懒加载相似
,用的到的时候才会加载,不用就不会加载。

.inflate()方法:惰性加载,加载到视图中

源代码:

package com.example.layout;

import android.os.Bundle;
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;
import android.widget.TextView;

public class MainActivity extends Activity {


    TextView  title_tv;
    private Button button; 
    private ViewStub stub; 

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

        title_tv= (TextView) findViewById(R.id.title);
        title_tv.setText("自定义布局");

        button = (Button) findViewById(R.id.button);
        stub= (ViewStub) findViewById(R.id.stub);
        button.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                // TODO Auto-generated method stub
                stub.inflate(); 
            }
        });
    }
}
<?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" >

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

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

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="正文内容"
            android:textSize="18sp" />

        <include layout="@layout/c_progress" />
    </FrameLayout>

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="显示隐藏内容" />

    <ViewStub
        android:id="@+id/stub"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout="@layout/c_text" />

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

<!-- android:id="@+id/title01" -->    
    <TextView
        android:id="@+id/ret"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:text="返回"
        android:textColor="#ff0000"
        android:textSize="14sp" />

    <TextView
        android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="布局优化"
        android:textColor="#ff0000"
        android:textSize="18sp" />

    <TextView
        android:id="@+id/fun"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:layout_marginRight="10dp"
        android:text="功能"
        android:textColor="#ff0000"
        android:textSize="14sp" />

</RelativeLayout>
<?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:text="请稍候"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center" />

    -->

</merge>
<?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" >

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="隐藏 内容" />

</LinearLayout>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值