* fang@author
* Android布局原则:
* (1)尽量多使用LinearLayout和RelativeLAyout,不要使用AbsoluteLayout
* (2)在布局层次一样的情况下,建议使用LinearLayout代替RelativeLayout,因为LinearLayout性能要稍高一些
* (3)将可复用的组件抽取出来并通过include标签使用
* (4)使用viewStub标签来加载一些不常用的布局
* (5)使用merge标签减少布局的嵌套层次
* UI布局优化:
* <include/>的使用
* 作用:将共有的组件抽取出来单独放到一个xml文件中,然后使用include便签导入共用布局
* 效果:提高UI的制作和复用效率,也能保证制作的UI布局更加规整和易维护
如果include指定了id的话,就不能直接把它里面的控件当成主xml中的控件来直接获得了,必须先获得这个xml布局文件,再通过布局文件findViewById来获得其子控件。
代码如下
*
*使用merge合并UI布局
* 作用:合并UI布局,使用该标签能降低UI布局的嵌套层次
*
*场景一:布局根结点是FrameLayout且不需要设置background或padding属性,可以用merge代替
*场景二:某布局作为子布局被其他布局include时,使用merge当做该布局 的顶节点,这样在被引入时顶节点会被自动忽略
*
*merge的使用:使用include引入一个布局文件,由于布局文件是放在一个ViewGroup中的。如果这个ViewGroup没有指定背景或者padding
*之类的其实就没必要存在,而且会多出一个层级。可以使用merge标签代替这个标签来进行优化。
*
*使用ViewStub惰性加载:默认不加载到内存中
*
*作用:ViewStub标签同include标签一样可以用来引入一个外部布局,
*不同的是,Viewstub引入的布局默认不会扩张,既不会占用显示也不会占用位置,从而在解析layout时节省cpu和内存
*
*/
activity_main.xml
<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="com.example.android_ui.MainActivity" > <include layout="@layout/common_title" /> <FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<include layout="@layout/common_progressbar"/>"
<TextView
android:id="@+id/text04"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="正文内容" />
</FrameLayout> <Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="显示隐藏内容"/>
<ViewStub
android:id="@+id/viewStub"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout = "@layout/commontext"
/>
</LinearLayout>
commom_titile.xml
<?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:background="#000000"
android:paddingTop="10dp"
android:paddingBottom="10dp"
>
<TextView
android:id="@+id/text01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="15sp"
android:textColor="#ffffff"
android:layout_alignParentLeft="true"
android:layout_marginLeft="10dp"
android:layout_centerVertical="true"
android:text="返回"
/>
<TextView
android:id="@+id/text02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18sp"
android:textColor="#ffffff"
android:layout_centerInParent="true"
android:text="布局优化"
/>
<TextView
android:id="@+id/text03"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="15sp"
android:textColor="#ffffff"
android:layout_alignParentRight="true"
android:layout_marginRight="10dp"
android:layout_centerVertical="true"
android:text="功能"/> </RelativeLayout>
common_progressbar.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:id="@+id/progressBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<TextView
android:id="@+id/text_wait"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="加载中"
/>"
</merge>
commontext.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:orientation="vertical" >
<TextView
android:id="@+id/text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="隐藏内容"
/>
</LinearLayout>
main.java
package com.example.android_ui; import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewStub;
import android.widget.Button;
/**
public class MainActivity extends Activity { private Button btn;
private ViewStub stub; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn = (Button) findViewById(R.id.button);
stub = (ViewStub) findViewById(R.id.viewStub);
btn.setOnClickListener(new OnClickListener() { @Override
public void onClick(View v) {
// TODO Auto-generated method stub
stub.inflate();//默认不加载该布局到内存,当点击按钮时,该布局才会加载到内存中
}
});
} }