Android中提供了五种布局,其中用的最多的就是:LinearLayout, RelativeLayout 和 FrameLayout这三种布局,在对某一界面进行布局时最先想到也是通过这三种来布局的,不过当某一界面过于复杂时,往往会有多层嵌套,可能嵌套层数过深超过5层,比如,当我们有一个需求是这样的:界面中的一个按钮的长度需要是屏幕宽度的一半,而且需要在任何屏幕下都是屏幕宽度的一半,这个需求我们往往想到的就是用LinearLayout的weightSum属性和child的layout_weight属性结合来用来达到效果,你可能这样实现:

单独给这个Button包一层LinearLayout来设置weightSum和layout_weight:

<RelativeLayout   xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center_horizontal"
        android:weightSum="1">
    <Button
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="0.5"
        android:text="Button"/>
    </LinearLayout>
</RelativeLayout>

最后也达到了效果:

不过这样未免太浪费了吧,如果我的视图更复杂一些,比如要实现下面这样的布局,都是需要按照屏幕的宽高来给控件设置宽高比例,那么如果真要实现那么你的layout文件一定非常大。

竖屏:



横屏:

可以看到,这的确是适配屏幕的,是按照屏幕的宽高来对控件进行不同比例的设置。

要完成上面这样的布局,如果采用通常方法来布局那么将非常复杂,庆幸的是Google推出了一种新的布局库,叫百分比布局库,可以在sdk目录下就可以找到对应的jar包,然后把它添加到项目中即可使用,即:

所以,我们采用百分比布局来实现上面这种布局。

百分比布局库android-percent-support

百分比布局库中提供了两种布局可以设置百分比:PercentRelativeLayout、PercentFrameLayout,为什么没有LinearLayout呢?因为LinearLayout可以根据weightSum和layout_weight这两个属性来对child进行很方便的布局适配。

这两个百分比布局都有以下九个布局属性,值都是用百分比来表示宽度、高度、margin值,使用时候需要父布局为百分比布局,child控件才可以使用这九个布局属性

  • app:layout_heightPercent
  • app:layout_widthPercent
  • app:layout_marginPercent
  • app:layout_marginTopPercent
  • app:layout_marginBottomPercent
  • app:layout_marginLeftPercent
  • app:layout_marginRightPercent
  • app:layout_marginStartPercent
  • app:layout_marginEndPercent

使用如下:

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

    <Button
        android:id="@+id/button"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:text="width=50%"
        app:layout_widthPercent="50%" />
</android.support.percent.PercentRelativeLayout>

上面这样就设置了Button的宽度是父布局宽度的一半,所以在不同屏幕下也永远是父布局宽度的一般,这样很方便的实现了而不需要用LinearLayout设置weightSum和layout_weight来控制。

用百分比布局实现上图布局

其实PercentRelativeLayout 和PercentFrameLayout就是多了九个布局属性的RelativeLayout 和FrameLayout,用法完全和这两个布局一样,不过只有父布局是百分比布局(PercentRelativeLayout和PercentFrameLayout )的时候,child才能使用百分比布局属性进行布局,否则无效,如:

<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">
<LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    <Button
        android:id="@+id/button"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:text="width=50%"
        app:layout_widthPercent="50%" />
    </LinearLayout>
</android.support.percent.PercentRelativeLayout>

这样设置Button的宽度并不会是父布局宽度的一半,因为child用百分比只对父布局是百分比布局才有效。

好了,现在就直接贴下上面这幅图的布局xml:

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

    <Button
        android:id="@+id/button"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:text="width=50%"
        app:layout_widthPercent="50%" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_below="@id/button"
        android:background="#E91E63"
        android:text="height=20%,width=50%"
        app:layout_heightPercent="20%"
        app:layout_marginLeftPercent="50%"
        app:layout_widthPercent="50%" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_below="@id/button"
        android:background="#9C27B0"
        android:text="height=20%,width=50%"
        app:layout_heightPercent="20%"
        app:layout_widthPercent="50%" />

    <android.support.percent.PercentRelativeLayout
        android:id="@+id/percent1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/textView2">

        <TextView
            android:id="@+id/textView3"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:background="#03A9F4"
            android:text="height=20%,width=25%"
            app:layout_heightPercent="20%"
            app:layout_widthPercent="25%" />

        <TextView
            android:id="@+id/textView4"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_toRightOf="@id/textView3"
            android:background="#CDDC39"
            android:text="height=20%,width=50%"
            app:layout_heightPercent="20%"
            app:layout_widthPercent="50%" />

        <TextView
            android:id="@+id/textView5"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_toRightOf="@id/textView4"
            android:background="#FF5722"
            android:text="height=20%,width=25%"
            app:layout_heightPercent="20%"
            app:layout_widthPercent="25%" />
    </android.support.percent.PercentRelativeLayout>

    <android.support.percent.PercentRelativeLayout
        android:id="@+id/percent2"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_below="@id/percent1"
        android:background="#FFCCBC"
        android:gravity="center_horizontal"
        app:layout_widthPercent="50%">

        <TextView
            android:id="@+id/textView6"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:background="#03A9F4"
            android:text="height=20%,width=25%"
            app:layout_heightPercent="20%"
            app:layout_marginTopPercent="10%"
            app:layout_widthPercent="50%" />

        <TextView
            android:id="@+id/textView7"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_below="@id/textView6"
            android:background="#CDDC39"
            android:text="height=20%,width=50%"
            app:layout_heightPercent="20%"
            app:layout_marginTopPercent="10%"
            app:layout_widthPercent="50%" />
    </android.support.percent.PercentRelativeLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@id/percent1"
        android:layout_toRightOf="@id/percent2"
        android:orientation="vertical">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:weightSum="10">

            <Button
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="2.5"
                android:text="B" />

            <Button
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="2.5"
                android:text="B" />

            <Button
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="5"
                android:text="Button" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center_horizontal"
            android:weightSum="1">

            <Button
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="0.5"
                android:text="Button" />
        </LinearLayout>
    </LinearLayout>

</android.support.percent.PercentRelativeLayout>

Android百分比布局支持库(android-percent-support)的更多相关文章

  1. Android百分比布局支持库介绍——com.android.support:percent(转)

    转载自http://www.apkbus.com/forum.php?mod=viewthread&tid=244752&extra=&_dsign=0b699c42 在此之前 ...

  2. Android 自带图标库 android.R.drawable

    在xml文件中调用. android:title="@string/secure_connect"android:orderInCategory="100"an ...

  3. Android - 加入Android的OpenCV依赖库(Android Dependencies) 问题

    加入Android的OpenCV依赖库(Android Dependencies) 问题 本文地址: http://blog.csdn.net/caroline_wendy 假设想要加入OpenCV的 ...

  4. Android 百分比布局库(percent-support-lib) 解析与扩展

    转载请标明出处: http://blog.csdn.net/lmj623565791/article/details/46695347: 本文出自:[张鸿洋的博客] 一.概述 周末游戏打得过猛,于是周 ...

  5. Android百分比布局方案

    百分比布局让其中的控件在指定高度,宽度,margin时使用屏幕宽高的百分比,不使用dp,px.这样一套布局可以适应多个屏幕,方便适配.如: app:layout_heightPercent=" ...

  6. Android百分比布局成功导入及简单使用

    最近学习第一行代码第二版这本书,里面有介绍百分比布局的使用,经过一番摸索,终于是成功导入了百分比布局 就是这样,appcompat是25.3.1,那么百分比布局percent也是25.3.1 这样便是 ...

  7. [原]ffmpeg编译android 硬解码支持库 libstagefright

    最近花了一天时间将ffmpeg/tools/build_stagefright执行成功,主要是交叉编译所需要的各种动态库的支持没链接上,导致各种报错,基本上网络上问到的问题我都碰到了,特此记录下来. ...

  8. android xml 布局文件中 android:ems="10"

    宽度为10个字符的宽度 xml中 android:ems属性 ,作为EditText 默认生成 的属性,其含义是需要编辑的 字符串长度 .设置为10时,最多编辑 10个em ,一个em单位是 两个in ...

  9. android support Percent支持库开发

    Android的布局支持百分比的设置进行开发,来学习如何去实现它,不过看起来会像网页的设置,比如宽度的设置属性是`layout_widthPercent`.在此之前,我们一般都会设置Linearlay ...

随机推荐

  1. Android Multimedia框架总结(二十)MediaCodec状态周期及Codec与输入/输出Buffer过程(附实例)

    转载请把头部出处链接和尾部二维码一起转载,本文出自逆流的鱼yuiop:http://blog.csdn.net/hejjunlin/article/details/53183718 前言:前面几节都是 ...

  2. property干嘛的

    >>> import datetime >>> class c(): @property def noww(self): return datetime.datet ...

  3. Java异常处理-----Throwable类

    Throwable类 1.toString() 输出该异常的类名. 2.getMessage() 输出异常的信息,需要通过构造方法传入异常信息(例如病态信息). 3.printStackTrace() ...

  4. 修改CUSTOM.PLL文件调用客户化FORM&修改标准FORM

    修改custom.pll文件里 的过程event:参考例子如下,修改好后上传至$AU_TOP/resource 运行编译frmcmp_batch CUSTOM apps/apps module_typ ...

  5. 向VS中添加个PATH怎么样?

    属性中,有个调试的目录,向"环境"中添加: PATH="your path";$(PATH) 可以调试用一下.

  6. 全文检索Lucene (2)

    接着全文检索Lucene (1) . 下面我们来深入的研究一下,如何使用Lucene! 从全文检索Lucene (1)中我们可以看出,Lucene就好比一个双向的工作流,一方面是对索引库的维护,另一方 ...

  7. Cocoa惯性思维调试一例

    大熊猫猪·侯佩原创或翻译作品.欢迎转载,转载请注明出处. 如果觉得写的不好请多提意见,如果觉得不错请多多支持点赞.谢谢! hopy ;) 人总有惯性思维,在编程调试里也不例外.你总以为错误是显然的那一 ...

  8. 【一天一道LeetCode】#137. Single Number II

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...

  9. 03-Git常用命令演示、冲突演示

    Git常用命令演示 Git的的思想其实和SVN还是蛮像的,可以参考之前svn文章一起加深了解. 新建一个user2目录,clone下代码. 修改readme.txt git status 可以看到re ...

  10. 在golang中使用leveldb

    leveldb是一个很强悍的kv数据库,自然,我也希望能在go中使用. 如果有官方的go leveldb实现,那我会优先考虑,譬如这个,但是该库文档完全没有,并且在网上没发现有人用于实战环境,对其能否 ...