Android相关

安卓百分比布局

2017-09-12  本文已影响75人  Condor_c33a

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%"

实例

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"/>

上一篇 下一篇

猜你喜欢

热点阅读