1、儘量多使用LinearLayout(線性佈局)和RelativeLayout(相對佈局),不要使用AbsoluteLayout(絕對佈局)
2、在佈局層次一樣的情況下,建議使用LinearLayout代替RelativeLayout,因爲LinearLayout性能要稍微高一點
3、將可複用的組件抽取出來並通過include標籤使用
4、使用ViewStub標籤來加載一些不常使用的佈局
5、使用merge標籤減少佈局的嵌套層次
<include/>的使用
作用:將共用的組件抽取出來單獨放到一個xml文件中,然後使用include標籤導入共用佈局。
效果:提高UI的製作和複用效率,也能保證製作的UI佈局更加規整和易維護。
使用實例:
1、公共佈局
<?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:paddingTop="10dp"
android:paddingBottom="10dp"
android:background="#000000" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_marginLeft="10dp"
android:layout_centerVertical="true"
android:textSize="18sp"
android:textColor="#ffffff"
android:text="返回" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:textSize="22sp"
android:textColor="#ffffff"
android:text="佈局優化" />
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginRight="10dp"
android:layout_centerVertical="true"
android:textSize="18sp"
android:textColor="#ffffff"
android:text="功能" />
</RelativeLayout>
2、在其他佈局中使用公共佈局<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" >
<include
layout="@layout/public_title"/>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:gravity="center"
android:text="正文內容"
android:textSize="30sp" />
</RelativeLayout>
3、在代碼中使用公共佈局中的控件package com.cx.layouttest;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class MainActivity extends Activity {
private TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = (TextView) findViewById(R.id.textView2);
textView.setText("修改標題內容");
}
}
使用merge合併UI佈局作用:合併UI佈局,使用該標籤能降低UI佈局的嵌套層次
場景(1):佈局根節點是FrameLayout且不需要設置background或padding等屬性,可以用merge代替
場景(2):某佈局作爲子佈局被其他佈局include時,使用merge當作該佈局的定節點,這樣在被引入時定節點會自動被忽略
使用實例:
1、merge佈局文件
<?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/progressBar1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center" />
</merge>
2、Activity佈局文件<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" >
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="正文內容"
android:textSize="30sp" />
<include
layout="@layout/merge"/>
</FrameLayout>
</LinearLayout>
3、Activity頁面代碼package com.cx.layouttest;
import android.app.Activity;
import android.os.Bundle;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//使用merge合併UI佈局
setContentView(R.layout.main_merge);
}
}
使用ViewStub惰性加載作用:ViewStub標籤同include標籤一樣可以用來引入一個外部佈局,不同的是,ViewStub引入的佈局默認不會擴張,既不會佔用顯示也不會佔用位子,從而在解析layout時節省cpu和內存。
詳細參考:http://blog.csdn.net/hitlion2008/article/details/6737537
使用實例:
1、要隱藏的佈局文件
<?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/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="隱藏內容" />
</LinearLayout>
2、Activity佈局文件
<?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" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="顯示隱藏內容" />
<ViewStub
android:id="@+id/stub"
android:layout="@layout/view_stub"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
3、ViewStub在Activity中使用package com.cx.layouttest;
import android.annotation.TargetApi;
import android.app.Activity;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewStub;
import android.widget.Button;
public class MainActivity extends Activity {
private Button button;
private ViewStub viewStub;
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//使用merge合併UI佈局
setContentView(R.layout.main_viewstub);
button = (Button) findViewById(R.id.button1);
viewStub = (ViewStub) findViewById(R.id.stub);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
viewStub.inflate();
}
});
}
}
在上面的代碼中,運行程序後點擊按鈕,顯示隱藏內容後,再點擊按鈕系統直接崩潰。這是因爲ViewStub只能Inflate一次,之後會被置空,所以無法指望後面接着使用ViewStub來控制佈局。所以當需要在運行時不止一次的顯示和隱藏某個佈局,那麼ViewStub是做不到的。這時就只能使用View的可見性來控制了。源碼下載