Android進階——百分比佈局及擴展

一、Android官方推出的百分比佈局的使用

1、導入依賴

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

2、提供瞭如下的屬性

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

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

二、Android官方增強版百分比佈局的使用——推薦使用

注:在官方的基礎上增加了佈局PercentLinearLayout,支持百分比設置正方形,未改變官方原有的使用,支持設置字體的百分比,因此更推薦使用
1、導入依賴

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
對於值可以取:10%w , 10%h , 10% , 10%sw , 10%sh    

4、使用實例

<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>

效果圖

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

效果圖

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

效果圖

    <ImageButton
        android:src="@mipmap/ic_launcher"
        android:scaleType="fitXY"
        percent:layout_heightPercent="50%sh"
        percent:layout_widthPercent="50%sw" />  
效果好像與直接使用xx%差不多,但是交換過來好像就不一樣了

效果圖

使用總結:

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

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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章