1. Recycle機制
- 原理便是維護一個對象池,將不再使用但是可能再次使用的 對象引用 保留在這個對象池里,下次需要的時候來到這個對象池獲取。
- Android經常使用這個機制,例如 Message 類,特地注意一下,由于這個機制, 使用 Message 時,不能調用其
recycle()
方法,這會導致 Message 內部的鏈表(該鏈表用來存儲Message對象)變成循環鏈表,Message 的Recycle機制將會失效。 - 詳細可見 TiouLims 的文章
2. 使用android support Percent支持庫
現在有兩種布局支持百分比
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%"
控件距離父布局結束邊的距離:app:layout_marginEndPercent="x%"-
實例
<android.support.percent.PercentRelativeLayout
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"><Button 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"/> </android.support.percent.PercentRelativeLayout>