1. 程式人生 > >android 百分比佈局percentFrameLayout,percentRelativeLayout的使用

android 百分比佈局percentFrameLayout,percentRelativeLayout的使用

這種佈局方式是google新新增的一種佈局,需要引入安卓的support相容包,這個包大家都很清楚,一般android的新特效新功能都會在support包中。

我們知道。在安卓的佈局中,只有LinearLayout才支援設定權重weight來按比例劃分控制元件的大小,其他佈局都不支援這種方式,這就顯得,有時候我們一些複雜點的佈局搞起來就有點繁瑣。

為此,android引入了一種全新的佈局方式來解決這個問題,---百分比佈局,簡單的理解就是在這個佈局的控制元件可以設定百分比啦,很高大上有木有,,,,

1.引入

compile'com.android.support:percent:24.2.1'
2.新建一個layout檔案
<?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">


    <Button
        android:text="1"
        android:layout_gravity="left|top"
        app:layout_widthPercent="50%"
        app:layout_heightPercent="50%"
        />

    <Button
        android:text="2"
        android:layout_gravity="right|top"
        app:layout_widthPercent="50%"
        app:layout_heightPercent="50%"
        />

    <Button
        android:text="3"
        android:layout_gravity="left|bottom"
        app:layout_widthPercent="50%"
        app:layout_heightPercent="50%"
        />

    <Button
        android:text="4"
        android:layout_gravity="right|bottom"
        app:layout_widthPercent="50%"
        app:layout_heightPercent="50%"
        />

</android.support.percent.PercentFrameLayout>
我們可以看到,這裡多了兩個新屬性---
app:layout_widthPercent="50%"
app:layout_heightPercent="50%"
這就是指定我們的控制元件和父佈局的百分之50.
上圖