安卓百分比布局

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

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 175,521评论 25 709
  • Day1: 在代码中通过R.string.hello_world可以获得该字符串的引用; 在XML中通过@stri...
    冰凝雪国阅读 5,327评论 0 5
  • 前言 在进行Android开发中,常常需要用到各种布局来进行UI的绘制,今天我们就来讲下Android开发中最常用...
    残月雨纷纷阅读 3,790评论 0 6
  • 暴雨、阵雨绵绵细雨,感觉呼吸都是潮湿的。一直不喜欢雨天,一下雨就心情忧郁低落。终于在这几天的雨里找到了答案,很多年...
    微乴问阅读 1,248评论 0 0
  • 成绩差到人绝望!太挫败了!调整方法便研究感悟体验类。不再单纯详细解释教材知识。发散研究运用审读材料能力锻炼
    无言好名字阅读 877评论 0 0