最近修改lint警告时发现一个针对LinerLayout的异常提示:“Set android:baselineAligned="false"
on this element for better performance”
网上查了一下android:baselineAligned=”false”,发现它是LinerLayout用来对齐文案的属性,默认值是true。具体可参见这篇文章:http://blog.youkuaiyun.com/chenbengang/article/details/48154057?ticket=ST-34523-bJ5ojNUHGwYXx7O3jds2-passport.youkuaiyun.com
那么既然默认值是true,为什么lint要告诉我们把它置为false呢?
请看lint报错的问题布局:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:orientation="horizontal">
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical"
android:padding="10dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#99ffffff"
android:textSize="14dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:textColor="@color/white"
android:textSize="30dp" />
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical"
android:padding="10dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#99ffffff"
android:textSize="14dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:textSize="30dp" />
</LinearLayout>
</LinearLayout>
布局的最外层LinearLayout嵌套了两个子的LinearLayout,每个子LinearLayout又嵌套了两个TextView;
Set android:baselineAligned="false"
on this element for better performance这个提示就是针对最外层的LinerLayout的;
原因是:当LinerLayout的子View都是ViewGroup(自定义控件除外)时,Lint认为它的子View已经不需要基准线对齐了,为了不让LinerLayout去做无用的计算对齐的操作,提出了如上警告,修改掉之后就可以提高性能。
实编程中,我们应该时刻注意,如果是不需要用到android:baselineAligned的地方,都可以设置成false,这样也可以提高性能!(个人觉得基准线对齐在英文里面用的比较多,中文里面基本用不到……)
本文仅代表作者的个人见解,如有不足,请各位斧正,谢谢!