android开发笔记之com.android.support:percent

背景

最近在给别的部门的人做项目,发现他们项目中使用了比例控件,然后我再度娘了一下,发现这个东东就是基本的控件上加了一个按比例显示长度的几个属性,使用起来非常简单.

Demo的简单说明

第一步:添加库文件
对于AndroidStudio来说,也就是在build.gradle文件中添加库

just add percent support library to your project

dependencies {
    compile 'com.android.support:percent:22.2.0'
}

第二步:在对应的布局文件中添加比例控件:

主要为:PercentRelativeLayout,PercentLinearLayout,PercentFrameLayout

其中使用比例数值的属性为:

heightPercent :百分比表示高度
widthPercent :百分比表示宽度
marginBottomPercent :百分比表示底部的间隔
marginEndPercent:百分比表示距离最后一个View之间的间隔
marginLeftPercent:百分比表示左边的间隔
marginPercent :百分比表示View之间的间隔
marginRightPercent:百分比表示右边的间隔
marginStartPercent:百分比表示距离第一个View之间的间隔
marginTopPercent:百分比表示顶部的间隔

我使用的为PercentRelativeLayout:

<?xml version="1.0" encoding="utf-8"?>
<android.support.percent.PercentRelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <View
        android:id="@+id/top_left"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_alignParentTop="true"
        android:background="#ff44aacc"
        app:layout_heightPercent="25%"
        app:layout_widthPercent="50%" />

    <View
        android:id="@+id/top_right"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_alignParentTop="true"
        android:layout_toRightOf="@+id/top_left"
        android:background="#ffe40000"
        app:layout_heightPercent="25%"
        app:layout_widthPercent="50%" />

    <View
        android:id="@+id/middle"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_below="@+id/top_left"
        android:background="#ff00ff22"
        app:layout_heightPercent="30%" />

    <View
        android:id="@+id/bottom_left"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_below="@+id/middle"
        android:background="#ff60f5f2"
        app:layout_marginTopPercent="3%"
        app:layout_marginLeftPercent="5%"
        app:layout_heightPercent="30%" />

</android.support.percent.PercentRelativeLayout>

查看效果图:

这里写图片描述

这里写图片描述

参考资料

1.android-percent-support-lib-sample
https://github.com/JulienGenoud/android-percent-support-lib-sample
2.ZJ_PercentSupportDemo
https://github.com/zhengjiong/ZJ_PercentSupportDemo
3.Android进阶系列之Percent Support Library使用详解
http://blog.csdn.net/sw5131899/article/details/53995796
4.Android Support库百分比布局 com.android.support:percent:22.2.0 附带Eclipse教程
http://blog.csdn.net/zsc357448181/article/details/46698261

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

hfreeman2008

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值