一概述
安卓開發(fā)過(guò)程中一直比較無(wú)賴適配的問(wèn)題个少,感到比較迷茫踏揣,一般的手機(jī)開發(fā)布局只是做到簡(jiǎn)單的適配锻全,有的時(shí)候我們覺(jué)得網(wǎng)頁(yè)的適配為什么簡(jiǎn)單狂塘,其實(shí)有了解過(guò)的小盆友知道录煤,網(wǎng)頁(yè)的界面使用百分比計(jì)算的,那么這樣的話荞胡,頁(yè)面大小的適配妈踊,是不是變得很簡(jiǎn)單,其實(shí)泪漂,近期安卓也是新增了百分比布局廊营,讓吃瓜的觀眾很是激動(dòng)。
此庫(kù)提供了兩種布局供大家使用:
PercentRelativeLayout萝勤、PercentFrameLayout赘风,通過(guò)名字就可以看出,這是繼承自FrameLayout和RelativeLayout兩個(gè)容器類纵刘;
支持的屬性有:
layout_widthPercent邀窃、layout_heightPercent、
layout_marginPercent假哎、layout_marginLeftPercent瞬捕、
layout_marginTopPercent、layout_marginRightPercent舵抹、
layout_marginBottomPercent肪虎、layout_marginStartPercent、layout_marginEndPercent惧蛹。
可以看到支持寬高扇救,以及margin。
二香嗓、簡(jiǎn)單的使用:
首先記得在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>
三總結(jié):
使用百分比布局會(huì)是的很多的布局變的很簡(jiǎn)單方便迅腔,而且在適配布局的大小上也不用那么復(fù)雜的計(jì)算,對(duì)許多朋友都很有幫助靠娱,有不好的地方多多指出沧烈,后期多多改正!
四引用:
https://github.com/hongyangAndroid/android-percent-support-extend