学习Android百分百布局库

 前几天看到无意中看到鸿洋大神的博客 博客地址:http://blog.csdn.net/lmj623565791/article/details/46767825 

有点小激动 !好吧,开始学习~ 一共四个类+一个attrs.xml 属性

PercentFrameLayout
PercentLinearLayout
PercentRelativeLayout
PercentLayoutHelper
这四个类代码我就不贴了,说一下使用方式


PercentLinearLayout 其实就是继承LinearLayout而来的就发和LinearLayout一样,下面看我布局

<?xml version="1.0" encoding="utf-8"?>
<com.lly.percentlyout.view.PercentLinearLayout 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:orientation="vertical" >

    <Button
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_heightPercent="10%"
        app:layout_widthPercent="60%"
        android:background="@android:color/black"
        android:text="宽:60% 高:10%"
        android:textColor="@android:color/white" />

    <Button
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_heightPercent="20%"
        app:layout_widthPercent="40%"
        android:background="@android:color/holo_green_dark"
        android:text="宽:40% 高:20%" />

    <Button
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_heightPercent="50%"
        app:layout_widthPercent="80%"
        android:background="@color/red"
        android:text="宽:80% 高:50%" />

    <Button
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_heightPercent="20%"
        app:layout_widthPercent="20%"
        android:background="@color/blue"
        android:text="宽:20% 高:20%"
        android:textColor="@android:color/white" />


</com.lly.percentlyout.view.PercentLinearLayout>
</pre><pre name="code" class="html">



如图(竖屏):



横屏:



怎么样~ 百分比显示是不是很爽~  让我先去打一把排位压压惊~  


其他还有PercentFrameLayout和PercentRelativeLayout2个方式~ 用法都一样


如:PercentRelativeLayout


<?xml version="1.0" encoding="utf-8"?>
<com.lly.percentlyout.view.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" >

    <TextView
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        app:layout_marginLeftPercent="5%w"
        app:layout_marginTopPercent="10%h"
        app:layout_heightPercent="30%"
        app:layout_widthPercent="30%"
        android:background="@color/red"
        android:gravity="center"
        android:text="高:30%,宽50%" />

</com.lly.percentlyout.view.PercentRelativeLayout>



如图:



  PercentFrameLayout 就不写了~ 


  下面说一些常用的属性:

  app:layout_heightPercent="50%w" (控件的高度以容器的宽的50%为参考)
  app:layout_widthPercent="50%w" (控件的高度以容器的宽的50%为参考)

  app:layout_heightPercent="50%h" (控件的高度以容器的高的50%为参考)
  app:layout_widthPercent="50%h" (控件的高度以容器的高的50%为参考)

  app:layout_heightPercent="50%" (控件的高度以容器的高的50%为参考)
  app:layout_widthPercent="50%" (控件的高度以容器的宽的50%为参考)


  //带指定参考值的设置Margin  w:代表容器宽度  h: 代表容器高度
  app:layout_marginPercent="20%w" 设置控件Margin以容器的宽的20%为参考
  app:layout_marginPercent="20%h" 设置控件Margin以容器的高的20%为参考
  
  其他还有
  app:layout_marginLeftPercent="20%w"
  app:layout_marginTopPercent="20%w"
  app:layout_marginRightPercent="20%w"
  app:layout_marginBottomPercent="20%w"
 
  app:layout_marginLeftPercent="20%h"
  app:layout_marginTopPercent="20%h"
  app:layout_marginRightPercent="20%h"
  app:layout_marginBottomPercent="20%h"

  //不带指定参考值的设置Margin  默认上下间距为容器的高度,左右间距为容器的宽度

  app:layout_marginPercent="20%"  设置控件的Margin上下间距为容器高的20%,左右间距为容器宽的20%

  其他还有
  app:layout_marginLeftPercent="20%"
  app:layout_marginTopPercent="20%"
  app:layout_marginRightPercent="20%"
  app:layout_marginBottomPercent="20%"

  恩~ 那就先介绍到这里~ 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值