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

猜你喜欢

转载自blog.csdn.net/hfreeman2008/article/details/78872667
今日推荐