每日一记:REcycle机制和android support Percent支持库

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>
    
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容