Android進(jìn)階——百分比布局及擴(kuò)展

CSDN博客地址:http://blog.csdn.net/wo_ha/article/details/54138417
一、Android官方推出的百分比布局的使用

1、導(dǎo)入依賴(lài)

dependencies {
    compile 'com.android.support:percent:25.0.+'
}

2、提供了如下的屬性

支持的布局有:PercentRelativeLayout,PercentFrameLayout
屬性如下:
heightPercent
widthPercent
marginBottomPercent
marginEndPercent
marginLeftPercent
marginPercent
marginRightPercent
marginStartPercent
marginTopPercent

更多請(qǐng)參考:https://juliengenoud.github.io/android-percent-support-lib-sample/(需要正確上網(wǎng))

二、Android官方增強(qiáng)版百分比布局的使用——推薦使用

注:在官方的基礎(chǔ)上增加了布局PercentLinearLayout,支持百分比設(shè)置正方形,未改變官方原有的使用,支持設(shè)置字體的百分比,因此更推薦使用
1、導(dǎo)入依賴(lài)

dependencies {
    //...
    compile 'com.zhy:percent-support-extends:1.1.1'
}

2、支持的布局有

com.zhy.android.percent.support.PercentLinearLayout
com.zhy.android.percent.support.PercentRelativeLayout
com.zhy.android.percent.support.PercentFrameLayout

3、支持的屬性有

支持的屬性 :
layout_heightPercent
layout_widthPercent
layout_marginBottomPercent
layout_marginEndPercent
layout_marginLeftPercent
layout_marginPercent
layout_marginRightPercent
layout_marginStartPercent
layout_marginTopPercent
layout_textSizePercent
layout_maxWidthPercent
layout_maxHeightPercent
layout_minWidthPercent
layout_minHeightPercent
layout_paddingPercent
layout_paddingTopPercent
layout_paddingBottomPercent
layout_paddingLeftPercent
layout_paddingRightPercent
對(duì)于值可以取:10%w , 10%h , 10% , 10%sw , 10%sh    

4、使用實(shí)例

<com.zhy.android.percent.support.PercentRelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:percent="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <ImageButton
        android:src="@mipmap/ic_launcher"
        android:scaleType="fitXY"
        percent:layout_heightPercent="50%"
        percent:layout_widthPercent="50%" />
</com.zhy.android.percent.support.PercentRelativeLayout>

效果圖

Paste_Image.png
<ImageButton
    android:scaleType="fitXY"
    android:src="@mipmap/ic_launcher"
    percent:layout_heightPercent="50%w"
    percent:layout_widthPercent="50%" />

效果圖


Paste_Image.png
<ImageButton
    android:scaleType="fitXY"
    android:src="@mipmap/ic_launcher"
    percent:layout_heightPercent="50%"
    percent:layout_widthPercent="50%h" />

效果圖

Paste_Image.png
<ImageButton
    android:src="@mipmap/ic_launcher"
    android:scaleType="fitXY"
    percent:layout_heightPercent="50%sh"
    percent:layout_widthPercent="50%sw" />  

效果好像與直接使用xx%差不多,但是交換過(guò)來(lái)好像就不一樣了
效果圖

Paste_Image.png

使用總結(jié):

1.屬性就是在Android原有的屬性后增加Percent,如android:layout_height —>android:layout_heightPercent
2.百分號(hào)后面的單位,如10%w :占手機(jī)屏幕寬度的十分之一, 10%h:占手機(jī)屏幕高度的十分之一 , 10% :占手機(jī)屏幕寬/高度的十分之一, 10%sw、10%sh 與10%w、10%h基本相同

更多使用請(qǐng)參考:
https://github.com/hongyangAndroid/android-percent-support-extend
http://blog.csdn.net/lmj623565791/article/details/46767825

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容