android优化(三)---App调试之UI布局篇

android优化(三)---App调试之UI布局篇


使用抽象布局标签(include, viewstub, merge),具有去除不必要的嵌套和View节点、减少不必要的infalte及其他Layout方面可调优点,布局分析时候可以使用工具(hierarchy viewer和lint)

a.布局重用<include />

	<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
	    android:orientation="vertical"   
	    android:layout_width=”match_parent”  
	    android:layout_height=”match_parent”  
	    android:background="@color/app_bg"  
	    android:gravity="center_horizontal">  
	  
	    <include layout="@layout/titlebar"/>  
	  
	    <TextView android:layout_width=”match_parent”  
	              android:layout_height="wrap_content"  
	              android:text="@string/hello"  
	              android:padding="10dp" />  
	      ...  
	  
	</LinearLayout>  

b.减少视图层级<merge />

       <merge/>标签在UI的结构优化中起着非常重要的作用,它可以删减多余的层级,优化UI。<merge/>多用于替换FrameLayout或者当一个布局包含另一个时,<merge/>标签消除视图层次结构中多余的视图组。例如你的主布局文件是垂直布局,引入了一个垂直布局的include,这是如果include布局使用的LinearLayout就没意义了,使用的话反而减慢你的UI表现。这时可以使用<merge/>标签优化。

<merge xmlns:android="http://schemas.android.com/apk/res/android">    
	  <Button  
	        android:layout_width="fill_parent"   
	        android:layout_height="wrap_content"  
	        android:text="@string/add"/>  
	  
	  <Button  
	        android:layout_width="fill_parent"   
	        android:layout_height="wrap_content"  
	        android:text="@string/delete"/>  
</merge>
 案例1:
首先需要一个配置文件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" >
  <TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="merge标签使用" />
</RelativeLayout>
再来一个最简单的Activity,文件名MainActivity.java
public class MainActivity extends Activity {
     @Override
     protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
     }
}
android优化(三)---App调试之UI布局篇_第1张图片
布局文件activity_main.xml修改内容如下:
<merge 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" >
  <TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="merge标签使用" />
</merge>
android优化(三)---App调试之UI布局篇_第2张图片
经验总结:
子视图不需要指定任何针对父视图的布局属性,组件仅仅需要直接添加到父视图上用于显示。

所有的Activity视图的根节点都是frameLayout
<merge/>只可以作为xml 布局的根节点。
如果创建的Layout并不是用FramLayout作为根节点,而是LinerLayout等,就不能应用merge来优化UI结构。
#当应用Include或者ViewStub标签从外部导入xml结构时,可以将被导入的xml用merge作为根节点表示,这样当被嵌入父级结构中后可以很好的将它所包含的子集融合到父级结构中,而不会出现冗余的节点。
#在代码中使用LayoutInflater.Inflater()一个以merge为根元素的布局文件时候,需要使用View inflate (int resource, ViewGroup root, boolean attachToRoot)指定一个ViewGroup 作为其容器,并且要设置attachToRoot 为true。

c、需要时使用<ViewStub />

<ViewStub />标签最大的优点是当你需要时才会加载,使用他并不会影响UI初始化时的性能。各种不常用的布局想进度条、显示错误消息等可以使用<ViewStub/>标签,以减少内存使用量,加快渲染速度。<ViewStub />是一个不可见的,大小为0View<ViewStub/>标签使用如下:

	<ViewStub  
	    android:id="@+id/stub_import"  
	    android:inflatedId="@+id/panel_import"  
	    android:layout="@layout/progress_overlay"  
	    android:layout_width="fill_parent"  
	    android:layout_height="wrap_content"  
	    android:layout_gravity="bottom" />  </span>

当你想加载布局时,可以使用下面其中一种方法:

        ((ViewStub) findViewById(R.id.stub_import)).setVisibility(View.VISIBLE);  
	// or  
	View importPanel = ((ViewStub) findViewById(R.id.stub_import)).inflate(); 

当调用inflate()函数的时候,ViewStub被引用的资源替代,并且返回引用的view 这样程序可以直接得到引用的view而不用再次调用函数findViewById()来查找了。注:ViewStub目前有个缺陷就是还不支持 <merge /> 标签。



常见问题:

在RelativeLayout中使用include标签

但是却发现include进来的控件无法用layout_alignParentBottom="true"之类的标签来调整。这个真的非常恼火。其实解决方法非常简单,只要你在include的时候同时重载下layout_width和layout_height这两个标签就可以了。如果不重载,任何针对include的layout调整都是无效的!




你可能感兴趣的:(UI,标签,合并,调试,布局)