前几天看到无意中看到鸿洋大神的博客 博客地址: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>
如图:
下面说一些常用的属性:
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%"
恩~ 那就先介绍到这里~