1尽量多使用RelativeLayout来减少布局层级的嵌套
2多处相同的布局模块多用include进行引用;比如统一的标题栏(包括都有的主页返回按钮)
3使用标签来实现惰性加载,比如进度条再使用的时候才加载,不使用不加载
4使用可替代帧布局或相对布局减少布局层级的嵌套
标签示例代码
<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=".MainActivity">
<Button
android:id="@+id/btn_show"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Show"/>
<Button
android:id="@+id/btn_gone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="GONE"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ViewStub
android:id="@+id/viewstub"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout="@layout/view"/>
</LinearLayout>
</LinearLayout>
view布局
<?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">
<ProgressBar
android:id="@+id/progressBar1"
style="?android:attr/progressBarStyleLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
标签示例代码
未使用merge标签的布局
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@mipmap/ic_launcher"
android:scaleType="fitXY"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="GreenRobot"
android:layout_marginBottom="20dp"
android:textColor="#000"
android:background="#22000000"
android:padding="12dp"
android:layout_gravity="center_horizontal|bottom"/>
</FrameLayout>
使用merge标签的布局;减少了一个framlayout层级
<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@mipmap/ic_launcher"
android:scaleType="fitXY"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="GreenRobot"
android:layout_marginBottom="20dp"
android:textColor="#000"
android:background="#22000000"
android:padding="12dp"
android:layout_gravity="center_horizontal|bottom"/>
</merge>
深入了解merge
https://www.jianshu.com/p/fbcc6a17d11e