Android嵌套線性布局玩法坑解決方法
前言
嵌套線性布局大家應該都用的非常熟悉,畢竟這玩意理解起來也是真的簡單,而且如果熟悉的話這玩意開發(fā)起來的效率也是真的快,不用一下一下拖動。
但是這個玩意有個非常的問題,就是性能問題,而且人家性能問題是指數(shù)級別增加的,怎么回事呢,因為你如果一層一層的嵌套布局的話,系統(tǒng)在繪制的時候就是指數(shù)級別的繪制次數(shù),如果你只是嵌套了倆層那都還能接受的玩,如果你一個界面控件很多,然后你又嵌套幾層線性布局,那這個時候性能就十分低下了。
詳解
- 看下面的代碼,就是一個十分典型的線性嵌套布局,用起來是很爽,無腦套,但是系統(tǒng)可不少受
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
</LinearLayout>
</LinearLayout>
</LinearLayout>
</LinearLayout>
</LinearLayout>
</FrameLayout>為什么會讓性能降低的怎么嚴重呢?
結論是繪制次數(shù)太多,主要是線性布局會造成這個問題,線性布局會對子view進行二次測量甚至三次測量。
比如:
1.LinearLayout寬度為wrap_content,因此它將選擇子View的最大寬度為其最后的寬度
2.但是有個子View的寬度為match_parent,意思它將以LinearLayout的寬度為寬度,這就陷入死循環(huán)了
3.因此這時候, LinearLayout 就會先以0為強制寬度測量一下子View,并正常地測量剩下的其他子View,然后再用其他子View里最寬的那個的寬度,二次測量這個match_parent的子 View,最終得出它的尺寸,并把這個寬度作為自己最終的寬度。
4.這是對單個子View的二次測量,如果有多個子View寫了match_parent ,那就需要對它們每一個都進行二次測量。
5.除此之外,如果在LinearLayout中使用了weight會導致測量3次甚至更多,重復測量在Android中是很常見的
所以我們的嵌套對性能影響是指數(shù)級別的,比如線性套線性套線性這種。
以上就是Android嵌套線性布局玩法坑解決方法的詳細內容,更多關于Android 嵌套線性布局的資料請關注腳本之家其它相關文章!
相關文章
Android開發(fā)之使用ExifInterface獲取拍照后的圖片屬性
這篇文章主要介紹了Android開發(fā)之使用ExifInterface獲取拍照后的圖片屬性,較為詳細的分析了ExifInterface類操作圖片的具體使用技巧,需要的朋友可以參考下2016-01-01
Android之線程池ThreadPoolExecutor的簡介
今天小編就為大家分享一篇關于Android之線程池ThreadPoolExecutor的簡介,小編覺得內容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧2019-03-03
android播放視頻時在立體聲與單聲道之間切換無變化原因分析及解決
使用第三方視頻播放器,有立體聲與單聲道之間切換,發(fā)現(xiàn)切換后無作用,原因是由于在HAL層默認沒有處理上層發(fā)的stereo 轉mono的命令,具體的解決方法如下2013-06-06
Android OnCreate()中獲取控件高度與寬度兩種方法詳解
這篇文章主要介紹了Android OnCreate()中獲取控件高度與寬度兩種方法詳解的相關資料,這里提供了兩種方法,大家可以都看下,需要的朋友可以參考下2016-12-12

