轉載請標明出處:
http://blog.csdn.net/lmj623565791/article/details/46767825;
本文出自:【張鴻洋的博客】java
上週一咱們發佈了Android 百分比佈局庫(percent-support-lib) 解析與擴展中對percent-support這個庫進行了解析和添加了PercentLinearLayout
的支持。android
那麼爲何本篇博客的存在的意義是什麼呢?git
首先咱們回顧下百分比佈局庫的用法,提供了PercentRelativeLayout
、PercentFrameLayout
供你們在編寫的時候,對於如下屬性:github
layout_widthPercent
、layout_heightPercent
、
layout_marginPercent
、layout_marginLeftPercent
、
layout_marginTopPercent
、layout_marginRightPercent
、
layout_marginBottomPercent
、layout_marginStartPercent
、layout_marginEndPercent
。app
可使用百分比進行設置寬、高、邊距,的確給咱們在適配上提供了極大的便利,可是在使用過程當中,以爲存在一些場景沒法獲得知足。什麼場景呢?下面我舉幾個例子。eclipse
當使用圖片時,沒法設置寬高的比例maven
好比咱們的圖片寬高是200*100的,咱們在使用過程當中咱們設置寬高爲20%、10%,這樣會形成圖片的比例失調。爲何呢?由於20%參考的是屏幕的寬度,而10%參考的是屏幕的高度。佈局
很難使用百分比定義一個正方形的控件字體
好比,我如今界面的右下角有一個FloatingActionButton
,我但願其寬度和高度都爲屏幕寬度的10%,很難作到。this
一個控件的margin四個方向值一致
有些時候,我設置margin,我但願四邊的邊距一致的,可是若是目前設置5%,會形成,上下爲高度的5%,左右邊距爲寬度的5%。
綜合上述這些問題,能夠發現目前的percent-support-lib並不能徹底知足咱們的需求,因此咱們考慮對其進行擴展。說白了,咱們就希 望在佈局的時候能夠本身設定參考看度仍是高度,好比上述2,咱們對於寬高能夠寫成10%w,10%w。也就是在不改變原庫的用法的前提下,添加一些額外的 支持。
目前我初步對該庫進行了改寫,github地址:android-percent-support-extend,對於官方庫,作了以下的改變:
PercentLinearLayout
支持百分比指定特定的參考值,好比寬度或者高度。
例如:app:layout_heightPercent="50%w"
, app:layout_marginPercent="15%w"
,
app:layout_marginBottomPercent="20%h"
.
PercentLinearLayout
的外層使用ScrollView,不過對於寬度的百分比參考的就是android.R.id.content的高度(由於,沒法參考父控件的高度,父控件的高度理論上依賴於子View高度,且模式爲UNSPECIFIED)。對於如何導入,也是至關的簡單,android studio的用戶,直接:
dependencies { //... compile 'com.zhy:percent-support-extends:1.0.1' }
不須要導入官方的percent-support-lib了。
對於的三個類分別爲:
com.zhy.android.percent.support.PercentLinearLayout com.zhy.android.percent.support.PercentRelativeLayout com.zhy.android.percent.support.PercentFrameLayout
對於eclipse的用戶:github上自行下載源碼,就幾個類和一個attrs.xml,也能夠在bintray.com/percent-support-extends 下載相關文件。
下面看幾個具體的示例。
xml:
<?xml version="1.0" encoding="utf-8"?> <com.zhy.android.percent.support.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" android:orientation="vertical"> <com.zhy.android.percent.support.PercentFrameLayout android:layout_width="0dp" android:layout_height="0dp" android:layout_gravity="center" android:background="#ff44aacc" app:layout_heightPercent="50%w" app:layout_widthPercent="50%w"> <com.zhy.android.percent.support.PercentFrameLayout android:layout_width="0dp" android:layout_height="0dp" android:layout_gravity="center" android:background="#ffcc5ec7" app:layout_heightPercent="50%w" app:layout_widthPercent="50%w"> <TextView android:layout_width="match_parent" android:layout_height="match_parent" android:layout_gravity="center" android:background="#ff7ecc16" android:gravity="center" android:text="margin 15% of w" app:layout_marginPercent="15%w" /> </com.zhy.android.percent.support.PercentFrameLayout> </com.zhy.android.percent.support.PercentFrameLayout> <TextView android:layout_width="0dp" android:layout_height="0dp" android:layout_gravity="bottom|right" android:background="#44ff0000" android:gravity="center" android:text="15%w,15%w" app:layout_heightPercent="15%w" app:layout_marginPercent="5%w" app:layout_widthPercent="15%w"/> </com.zhy.android.percent.support.PercentFrameLayout>
xml:
<?xml version="1.0" encoding="utf-8"?> <com.zhy.android.percent.support.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="70%"/> <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:100%,height:10%" app:layout_heightPercent="10%" app:layout_widthPercent="100%"/> </com.zhy.android.percent.support.PercentRelativeLayout>
ok,例子都比較簡單,主要就一個佈局文件,能夠看出上述咱們能夠給寬度、高度,邊距等指定參考值爲寬度或者高度。這樣的話,在保證圖片寬、高比例、控件設置爲正方形等需求就沒問題了。
接下來還有個例子,功能主要是設置TextView對於textSize的百分比設置;以及對於ScrollView的支持。固然了,對於ScrollView的支持,這個理論上是不支持的,由於你們都清楚,若是PercentLinearLayout
在ScrollView中,那麼高度的模式確定是UNSPECIFIED
, 那麼理論上來講高度是無限制的,也就是依賴於子View的高度,而百分比佈局的高度是依賴於父View的高度的,全部是互斥的。而咱們支持是:考慮到編寫 代碼的時候,大多參考的是屏幕高度(android.R.id.content)的高度,因此若是在ScrollView中,編寫10%h,這個百分比是 依賴於屏幕高度的(不包括ActionBar的高度)。
xml:
<?xml version="1.0" encoding="utf-8"?> <ScrollView 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"> <com.zhy.android.percent.support.PercentLinearLayout 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="wrap_content" android:orientation="vertical"> <TextView android:layout_width="0dp" android:layout_height="0dp" android:background="#ff44aacc" android:gravity="center" android:text="width:60%,height:5%,ts:3%" android:textColor="#ffffff" app:layout_heightPercent="5%" app:layout_marginBottomPercent="5%" app:layout_textSizePercent="3%" app:layout_widthPercent="60%"/> <TextView android:layout_width="0dp" android:layout_height="0dp" android:background="#ff4400cc" android:gravity="center" android:text="width:70%,height:10%" android:textColor="#ffffff" app:layout_heightPercent="10%" app:layout_marginBottomPercent="5%" app:layout_widthPercent="70%"/> <TextView android:layout_width="0dp" android:layout_height="0dp" android:background="#ff44aacc" android:gravity="center" android:text="w:80%,h:15%,textSize:5%" android:textColor="#ffffff" app:layout_heightPercent="15%" app:layout_marginBottomPercent="5%" app:layout_textSizePercent="5%" app:layout_widthPercent="80%"/> <TextView android:layout_width="0dp" android:layout_height="0dp" android:background="#ff4400cc" android:gravity="center" android:text="width:90%,height:5%" android:textColor="#ffffff" app:layout_heightPercent="20%" app:layout_marginBottomPercent="5%" app:layout_widthPercent="90%"/> <TextView android:layout_width="match_parent" android:layout_height="0dp" android:background="#ff44aacc" android:gravity="center" android:text="width:100%,height:25%" android:textColor="#ffffff" app:layout_heightPercent="25%" app:layout_marginBottomPercent="5%" /> <TextView android:layout_width="match_parent" android:layout_height="0dp" android:background="#ff44aacc" android:gravity="center" android:text="width:100%,height:30%" android:textColor="#ffffff" app:layout_heightPercent="30%" app:layout_marginBottomPercent="5%" /> </com.zhy.android.percent.support.PercentLinearLayout> </ScrollView>
上面的第三個TextView的字體設置的就是5%(默認參考容器高度)。整個PercentLinearLayout在ScrollView中。ok~ 姑且這樣,因爲源碼比較簡單,你們能夠根據本身的實際需求去修改,前提儘量不要改變原有的功能。
原庫中全部的屬性的format爲fraction,可是因爲我指望的寫法有10%w,10%h,10%,沒有找到合適的format,就直接定義爲string了~string我能夠本身去解析~
<?xml version="1.0" encoding="utf-8"?> <resources> <declare-styleable name="PercentLayout_Layout"> <attr name="layout_widthPercent" format="string"/> <attr name="layout_heightPercent" format="string"/> <attr name="layout_marginPercent" format="string"/> <attr name="layout_marginLeftPercent" format="string"/> <attr name="layout_marginTopPercent" format="string"/> <attr name="layout_marginRightPercent" format="string"/> <attr name="layout_marginBottomPercent" format="string"/> <attr name="layout_marginStartPercent" format="string"/> <attr name="layout_marginEndPercent" format="string"/> <attr name="layout_textSizePercent" format="string"/> </declare-styleable> </resources>
若是看了上篇博文的話,應該清楚,對於自定義屬性的值是在PercentLayoutHelper.getPercentLayoutInfo(c, attrs)
中獲取的。
簡單看下修改後的代碼:
public static PercentLayoutInfo getPercentLayoutInfo(Context context, AttributeSet attrs) { PercentLayoutInfo info = null; TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.PercentLayout_Layout); String sizeStr = array.getString(R.styleable.PercentLayout_Layout_layout_widthPercent); PercentLayoutInfo.PercentVal percentVal = getPercentVal(sizeStr, true); if (percentVal != null) { if (Log.isLoggable(TAG, Log.VERBOSE)) { Log.v(TAG, "percent width: " + percentVal.percent); } info = info != null ? info : new PercentLayoutInfo(); info.widthPercent = percentVal; } //省略了獲取其餘的相似屬性 array.recycle(); return info; } private static final String REGEX_PERCENT = "^(([0-9]+)([.]([0-9]+))?|([.]([0-9]+))?)%([wh]?)$"; /** * widthStr to PercentVal * <br/> * eg: 35%w => new PercentVal(35, true) * * @param percentStr * @param isOnWidth * @return */ private static PercentLayoutInfo.PercentVal getPercentVal(String percentStr, boolean isOnWidth) { //valid param if (percentStr == null) { return null; } Pattern p = Pattern.compile(REGEX_PERCENT); Matcher matcher = p.matcher(percentStr); if (!matcher.matches()) { throw new RuntimeException("the value of layout_xxxPercent invalid! ==>" + percentStr); } int len = percentStr.length(); //extract the float value String floatVal = matcher.group(1); String lastAlpha = percentStr.substring(len - 1); float percent = Float.parseFloat(floatVal) / 100f; boolean isBasedWidth = (isOnWidth && !lastAlpha.equals("h")) || lastAlpha.equals("w"); return new PercentLayoutInfo.PercentVal(percent, isBasedWidth); }
首先咱們獲取自定義屬性的填寫的值,經過getPercentVal方法,在該方法內部經過正則校驗其合法性,若是合法,則將其拆解封裝成 PercentVal對象,該對象中記錄百分比值,已經知否參考寬度的布爾值(若是參考寬度則爲true,不然爲false)。對於沒有後綴w|h的,和 原庫的解析方式相同。
PercentVal對象以下:
public static class PercentVal { public float percent = -1; public boolean isBaseWidth; public PercentVal(float percent, boolean isBaseWidth) { this.percent = percent; this.isBaseWidth = isBaseWidth; } }
對於定義的自定義屬性獲取完成以後,剩下的無非是測量時候對於本來的LayoutParams中的寬度和高度的賦值作簡單的修改。參考上一篇的源碼,咱們直接看 PercentLayoutInfo.fillLayoutParams(params, widthHint, heightHint);
方法:
public void fillLayoutParams(ViewGroup.LayoutParams params, int widthHint, int heightHint) { // Preserve the original layout params, so we can restore them after the measure step. mPreservedParams.width = params.width; mPreservedParams.height = params.height; /* if (widthPercent >= 0) { params.width = (int) (widthHint * widthPercent); } if (heightPercent >= 0) { params.height = (int) (heightHint * heightPercent); }*/ if (widthPercent != null) { int base = widthPercent.isBaseWidth ? widthHint : heightHint; params.width = (int) (base * widthPercent.percent); } if (heightPercent != null) { int base = heightPercent.isBaseWidth ? widthHint : heightHint; params.height = (int) (base * heightPercent.percent); } if (Log.isLoggable(TAG, Log.DEBUG)) { Log.d(TAG, "after fillLayoutParams: (" + params.width + ", " + params.height + ")"); } }
本來的源碼比較簡單,只須要將widthHint/heightHint乘以百分比便可(見上代碼註釋),而咱們修改的也比較容易,首先判斷參考寬度仍是高度,而後乘以百分比(根據咱們的對象PercentVal的屬性)。
ok,大概的源碼修改就是上述的內容,有興趣的能夠直接查看源碼。
固然了,上述庫中確定還存在或多或少的問題,你們能夠fork完善下,或者直接留言提意見均可以。
github地址:android-percent-support-extend ,用法參考上文,或者README。歡迎star and fork 。
~~have a nice day ~~
ok~