Android百分比布局:PercentLayout

                   Android百分比布局:PercentLayout

1.百分比布局是什么
简单来说就是按照父布局的宽高进行百分比分隔,以此来确定视图的大小。听起来好像牛逼闪闪的样子,让我们用一种图来认识下:

Android百分比布局:PercentLayout_第1张图片


上图的根布局使用百分比相对布局,子View就可以使用百分比确定自己的宽高,还是挺简单的。
        2.百分比布局和可以使用百分比的属性
        - PercentRelativeLayout
        - PercentFrameLayout
        - PercentLinearLayout(非官方,个人开发者扩展支持)
        前两种布局是google出品,第三个是某位大神的自定义扩展,从名字都名看出来是什么了吧,就是多个Percent前缀而已,其实用法也和原始的3个布局差不多。接下来看看都支持什么百分比属性:
        - heightPercent
        - widthPercent
        - marginBottomPercent
        - marginEndPercent
        - marginLeftPercent
        - marginPercent
        - marginRightPercent
        - marginStartPercent
        - marginTopPercent
        以上的这些属性值支持百分比,基本上没什么难理解的地方。
        3.用法
        1. 添加依赖。如果使用官方支持库,则没有百分比线性布局。
compile 'com.android.support:percent:26.0.+'

 
  
需要支持线性布局的就去使用那个大神的扩展吧,github地址: https://github.com/JulienGenoud/android-percent-support-lib-sample 2. 将布局替换成百分比布局,拿相对布局举例,以前怎么用现在还怎么用,就换个名字而已
<android.support.percent.PercentRelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >
注意要自己添加一个命名空间(如图中第三行的地方)
3. 布局内子View的宽高写法,需要改造下,如图
<TextView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
   tools:layout_height="20%"
    tools:layout_width="20%"
    />

宽高定义为0dp,用百分比宽高来确定大小,当然这个百分之多少指的是父布局宽高的百分数






你可能感兴趣的:(android基础)