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>
為什么會讓性能降低的怎么嚴重呢?
結(jié)論是繪制次數(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嵌套線性布局玩法坑解決方法的詳細內(nèi)容,更多關(guān)于Android 嵌套線性布局的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Android編程之播放器MediaPlayer實現(xiàn)均衡器效果示例
這篇文章主要介紹了Android編程之播放器MediaPlayer實現(xiàn)均衡器效果,結(jié)合具體實例形式分析了Android調(diào)用MediaPlayer相關(guān)API構(gòu)造均衡器的具體步驟與相關(guān)功能實現(xiàn)方法,需要的朋友可以參考下2017-08-08Android自定義實現(xiàn)BaseAdapter的普通實現(xiàn)
這篇文章主要為大家詳細介紹了Android自定義實現(xiàn)BaseAdapter的普通實現(xiàn),感興趣的小伙伴們可以參考一下2016-08-08Crashlytics Android 異常報告統(tǒng)計管理(詳解)
下面小編就為大家?guī)硪黄狢rashlytics Android 異常報告統(tǒng)計管理(詳解)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-05-05