1. 程式人生 > >Android適配之百分比佈局

Android適配之百分比佈局

一概述
安卓開發過程中一直比較無賴適配的問題,感到比較迷茫,一般的手機開發佈局只是做到簡單的適配,有的時候我們覺得網頁的適配為什麼簡單,其實有了解過的小盆友知道,網頁的介面使用百分比計算的,那麼這樣的話,頁面大小的適配,是不是變得很簡單,其實,近期安卓也是新增了百分比佈局,讓吃瓜的觀眾很是激動。

此庫提供了兩種佈局供大家使用:

  • PercentRelativeLayout、PercentFrameLayout,通過名字就可以看出,這是繼承自FrameLayout和RelativeLayout兩個容器類;

  • 支援的屬性有:

    layout_widthPercent、layout_heightPercent、
    layout_marginPercent、layout_marginLeftPercent、
    layout_marginTopPercent、layout_marginRightPercent、
    layout_marginBottomPercent、layout_marginStartPercent、layout_marginEndPercent。

可以看到支援寬高,以及margin。

二、簡單的使用:
首先記得在build.gradle新增:

compile 'com.android.support:percent:22.2.0'

(一)PercentFrameLayout

<?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">
<TextView android:layout_width="0dp" android:layout_height="0dp" android:layout_gravity="left|top" android:background="#44ff0000" android:text="width:10%,height:10%" app:layout_heightPercent
="10%" app:layout_widthPercent="20%"/>
<TextView android:layout_width="0dp" android:layout_height="0dp" android:layout_gravity="right|top" android:background="#4400ff00" android:text="width:50%,height:20%" app:layout_heightPercent="20%" app:layout_widthPercent="50%"/> </android.support.percent.PercentFrameLayout>

(二) PercentRelativeLayout

<?xml version="1.0" encoding="utf-8"?>
<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"
    android:clickable="true">

    <TextView
        android:id="@+id/row_one_item_one"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_alignParentTop="true"
        android:background="#7700ff00"
        android:text="w:70%,h:20%"
        android:gravity="center"
        app:layout_heightPercent="20%"
        app:layout_widthPercent="70%"/>

    <TextView
        android:id="@+id/row_one_item_two"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_toRightOf="@+id/row_one_item_one"
        android:background="#396190"
        android:text="w:30%,h:20%"
        app:layout_heightPercent="20%"
        android:gravity="center"
        app:layout_widthPercent="30%"/>


    <ImageView
        android:id="@+id/row_two_item_one"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:src="@drawable/tangyan"
        android:scaleType="centerCrop"
        android:layout_below="@+id/row_one_item_one"
        android:background="#d89695"
        app:layout_heightPercent="50%"/>

    <TextView
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_below="@id/row_two_item_one"
        android:background="#770000ff"
        android:gravity="center"
        android:text="width:90%,height:20%"
        app:layout_heightPercent="20%"
        app:layout_widthPercent="90%"/>
</android.support.percent.PercentRelativeLayout>

三總結:
使用百分比佈局會是的很多的佈局變的很簡單方便,而且在適配佈局的大小上也不用那麼複雜的計算,對許多朋友都很有幫助,有不好的地方多多指出,後期多多改正!