安卓百分⽐布局
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="schemas.android/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%"
实例
margin属性值可以为百分比xmlns:android="schemas.android/apk/res/android"
xmlns:tools="schemas.android/tools"
xmlns:app="schemas.android/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"/>

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。