Android百分比布局

一概述
安卓开发过程中一直比较无赖适配的问题,感到比较迷茫,一般的手机开发布局只是做到简单的适配,有的时候我们觉得网页的适配为什么简单,其实有了解过的小盆友知道,网页的界面使用百分比计算的,那么这样的话,页面大小的适配,是不是变得很简单,其实,近期安卓也是新增了百分比布局,让吃瓜的观众很是激动。

此库提供了两种布局供大家使用:

  • PercentRelativeLayout、PercentFrameLayout,通过名字就可以看出,这是继承自FrameLayout和RelativeLayout两个容器类;

  • 支持的属性有:

layout_widthPercent、layout_heightPercent、
layout_marginPercent、layout_marginLeftPercent、
layout_marginTopPercent、layout_marginRightPercent、
layout_marginBottomPercent、layout_marginStartPercent、layout_marginEndPercent。

可以看到支持宽高,以及margin。

二、简单的使用:
首先记得在build.gradle添加:

compile 'com.android.support:percent:22.2.0'

(一)PercentFrameLayout

<?xml version="1.0" encoding="utf-8"?>
<android.support.percent.PercentFrameLayout
    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">

    <TextView
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_gravity="left|top"
        android:background="#44ff0000"
        android:text="width:10%,height:10%"
        app:layout_heightPercent="10%"
        app:layout_widthPercent="20%"/>

    <TextView
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_gravity="right|top"
        android:background="#4400ff00"
        android:text="width:50%,height:20%"
        app:layout_heightPercent="20%"
        app:layout_widthPercent="50%"/>
</android.support.percent.PercentFrameLayout>

(二) 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"
    android:clickable="true">

    <TextView
        android:id="@+id/row_one_item_one"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_alignParentTop="true"
        android:background="#7700ff00"
        android:text="w:70%,h:20%"
        android:gravity="center"
        app:layout_heightPercent="20%"
        app:layout_widthPercent="70%"/>

    <TextView
        android:id="@+id/row_one_item_two"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_toRightOf="@+id/row_one_item_one"
        android:background="#396190"
        android:text="w:30%,h:20%"
        app:layout_heightPercent="20%"
        android:gravity="center"
        app:layout_widthPercent="30%"/>


    <ImageView
        android:id="@+id/row_two_item_one"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:src="@drawable/tangyan"
        android:scaleType="centerCrop"
        android:layout_below="@+id/row_one_item_one"
        android:background="#d89695"
        app:layout_heightPercent="50%"/>

    <TextView
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_below="@id/row_two_item_one"
        android:background="#770000ff"
        android:gravity="center"
        android:text="width:90%,height:20%"
        app:layout_heightPercent="20%"
        app:layout_widthPercent="90%"/>
</android.support.percent.PercentRelativeLayout>

三总结:
使用百分比布局会是的很多的布局变的很简单方便,而且在适配布局的大小上也不用那么复杂的计算,对许多朋友都很有帮助,有不好的地方多多指出,后期多多改正!

四引用:
https://github.com/hongyangAndroid/android-percent-support-extend

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

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 173,596评论 25 708
  • 基本介绍 Android提供了Android-percent-support这个库,支持百分比布局,在一定程度上可...
    蓝枫zeke阅读 15,820评论 0 17
  • Google开始支持百分比的方式布局了已经大半年了吧(7个月??)https://github.com/Julie...
    CoderBigBear阅读 2,584评论 2 2
  • 本书主要分为三部分讲述: 第一部分:商业篇 第二部分:团队篇 第三部分:职业管理篇 第一部分:商业篇 Before...
    a1a88c3a5c84阅读 3,567评论 0 2
  • 清晨醒来,我总是偷偷从背后抱住你 在你还未睡醒的时候,摸摸肚皮 圆圆,怎么抱都没有边 我的胖先生,这个时候我就想起...
    叮咚的你阅读 164评论 4 3