瞎扯
第二天,繼續(xù)
接上篇,上篇主要簡單聊了下分析布局
今天還是差不多,優(yōu)化布局.
優(yōu)化布局:
就說說我知道的一些,歡迎補充
1.減少嵌套
這點上篇就講過了,
再提是因為這個的重要性吧,
安卓的嵌套大致兩種.
-
列表嵌套
這種呢.首先要相信一點.同向的情況下,90%的列表設(shè)計都是可以一個RecyclerView搞定的.
如果非要嵌套呢.那么嵌套RecyclerView
的adapter
,一定要注意緩存,不要每次
onBindViewHolder的時候都去重新創(chuàng)建一次,縱向套橫向其實算不上嵌套.
當然,還有事件沖突,所以,不到萬不得已,不要去嵌套,該懟的設(shè)計就得懟.
-
容器嵌套.
還是上篇提到的,
LinearLayout,RelativeLayout
等等瘋狂嵌套.
然后頁面渲染卡的一筆,然后被人吐槽,安卓卡死.好在現(xiàn)在出了約束布局
CoordinatorLayout
,雖然我也不大用,因為用不上.不過是優(yōu)化布局的神器.
性能上比RelativeLayout
更好.還能做一些RelativeLayout
不好做的事.
最重要的是它對屏幕的適配性.
2 .背景
對,就是背景.
我見過很多,現(xiàn)在還喜歡用.9圖做背景.真的是不推薦
優(yōu)化的地方:
(1).不要重復(fù)設(shè)置背景:
這是常常會被忽略的一件事情.經(jīng)常會等到做布局優(yōu)化的時候才會去改.
比如一個列表頁面:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:background="#fff"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="@+id/rv_content"
android:background="#ccc"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<TextView
android:id="@+id/tv_next"
android:layout_width="match_parent"
android:layout_height="48dp"
android:layout_alignParentBottom="true"
android:background="#ff7000"
android:gravity="center"
android:text="確定選擇"
android:textColor="#fff"
android:textSize="16dp"/>
</RelativeLayout>
然后列表條目的布局:
<TextView
android:id="@+id/tv_name"
android:layout_width="match_parent"
android:layout_height="44dp"
android:background="#fff"
android:gravity="center_vertical"
android:text="name" />
這種寫法我是見過的..
然后打開查看過度繪制,整個頁面都是紅的.
這里一共重復(fù)了4次背景.
- 第一層,window背景
- 第二層.RelativeLayout背景
- 第三層.ReyclerView背景
- 第四層.TextView背景
實際上.除了TextView,其他的背景都可以去掉.
去掉window背景
<item name="android:windowBackground">@android:color/transparent</item>
(2).盡量不要用圖片做控件背景.
除了啟動頁.和部分標簽性的背景,還有什么地方是值得用圖片做背景的
如果只是為了一個邊線,
這些,只是一個shape就解決了.
我基本很少找UI切背景.只有icon會找UI, .9已經(jīng)1年多沒用過了吧.
如果允許,最好使用svg
來作為icon和背景.
一.節(jié)省app大小,
二.不會失真.如果是圖片做背景.失真是很的難看.
三.節(jié)省內(nèi)存.圖片可是很占內(nèi)存的.
總結(jié)就是能用shape的或者canvas畫的,絕不用圖片,搞不定也最好用svg來搞.
(3)ViewStub merge
這兩個東西,優(yōu)化上來說,是很常用的
ViewStub
相當于一個占位符.必須調(diào)用了inflate 和setvisible,設(shè)置想要的渲染的布局layoutId.才會被渲染.合理運用.可以優(yōu)化頁面啟動速度.-
merge
這個必須用在xml文件的根級,.
image.png
不然是用不了的.這個能減少一層容器布局.不過RecyclerView的item布局不能用這個.
交流群:493180098,這是個很少吹水,交流學(xué)習(xí)的群.