1. Recycle機(jī)制
原理便是維護(hù)一個(gè)對(duì)象池,將不再使用但是可能再次使用的對(duì)象引用保留在這個(gè)對(duì)象池里奔缠,下次需要的時(shí)候來到這個(gè)對(duì)象池獲取。
Android經(jīng)常使用這個(gè)機(jī)制,例如Message類度气,特地注意一下,由于這個(gè)機(jī)制膨报, 使用Message時(shí)磷籍,不能調(diào)用其recycle()方法,這會(huì)導(dǎo)致Message內(nèi)部的鏈表(該鏈表用來存儲(chǔ)Message對(duì)象)變成循環(huán)鏈表现柠,Message的Recycle機(jī)制將會(huì)失效院领。
2. 使用android support Percent支持庫
現(xiàn)在有兩種布局支持百分比PercentRelativeLayout,PercentFrameLayout
需要在支持庫中加入compile 'com.android.support:percent:23.0.0'
如下
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:23.0.1'
compile 'com.android.support:percent:23.0.0'
}
在使用到
android.support.percent.PercentRelativeLayout
android.support.percent.PercentFrameLayout
的布局文件頭中需要加入
xmlns:app="http://schemas.android.com/apk/res-auto"
支持的屬性有
控件寬度:app:layout_widthPercent="x%"
控件高度:app:layout_heightPercent="x%"
控件距離父布局左邊的距離:app:layout_marginLeftPercent="x%"
控件距離父布局右邊的距離:app:layout_marginRightPercent="x%"
控件距離父布局上邊的距離:app:layout_marginTopPercent="x%"
控件距離父布局下邊的距離:app:layout_marginBottomPercent="x%"
控件距離父布局開始邊的距離:app:layout_marginStartPercent="x%"
控件距離父布局結(jié)束邊的距離:app:layout_marginEndPercent="x%"
實(shí)例
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
android:id="@+id/id_btn"
app:layout_widthPercent="25%"
app:layout_heightPercent="50%"
app:layout_marginLeftPercent="10%"
app:layout_marginTopPercent="5%"
android:text="i am a button"/>