android百分比扩展枯,Android 增强版百分比布局库 为了适配而扩展
一 概述
上周一我們發(fā)布了Android 百分比布局庫(percent-support-lib) 解析與擴(kuò)展中對(duì)percent-support這個(gè)庫進(jìn)行了解析和添加了PercentLinearLayout的支持。
那么為什么本篇博客的存在的意義是什么呢?
首先我們回顧下百分比布局庫的用法,提供了PercentRelativeLayout、PercentFrameLayout供大家在編寫的時(shí)候,對(duì)于以下屬性:
layout_widthPercent、layout_heightPercent、?layout_marginPercent、layout_marginLeftPercent、?layout_marginTopPercent、layout_marginRightPercent、?layout_marginBottomPercent、layout_marginStartPercent、layout_marginEndPercent。
可以使用百分比進(jìn)行設(shè)置寬、高、邊距,的確給我們?cè)谶m配上提供了極大的便利,但是在使用過程中,覺得存在一些場景無法得到滿足。什么場景呢?下面我舉幾個(gè)例子。當(dāng)使用圖片時(shí),無法設(shè)置寬高的比例
比如我們的圖片寬高是200*100的,我們?cè)谑褂眠^程中我們?cè)O(shè)置寬高為20%、10%,這樣會(huì)造成圖片的比例失調(diào)。為什么呢?因?yàn)?0%參考的是屏幕的寬度,而10%參考的是屏幕的高度。
很難使用百分比定義一個(gè)正方形的控件
比如,我現(xiàn)在界面的右下角有一個(gè)FloatingActionButton,我希望其寬度和高度都為屏幕寬度的10%,很難做到。
一個(gè)控件的margin四個(gè)方向值一致
有些時(shí)候,我設(shè)置margin,我希望四邊的邊距一致的,但是如果目前設(shè)置5%,會(huì)造成,上下為高度的5%,左右邊距為寬度的5%。
綜合上述這些問題,可以發(fā)現(xiàn)目前的percent-support-lib并不能完全滿足我們的需求,所以我們考慮對(duì)其進(jìn)行擴(kuò)展。說白了,我們就希望在布局的時(shí)候可以自己設(shè)定參考看度還是高度,比如上述2,我們對(duì)于寬高可以寫成10%w,10%w。也就是在不改變?cè)瓗斓挠梅ǖ那疤嵯?#xff0c;添加一些額外的支持。
二 擴(kuò)展的功能
目前我初步對(duì)該庫進(jìn)行了改寫,github地址:android-percent-support-extend,對(duì)于官方庫,做了如下的改變:不改變?cè)袔斓挠梅?/p>
添加了PercentLinearLayout
支持百分比指定特定的參考值,比如寬度或者高度。
例如:app:layout_heightPercent="50%w",?app:layout_marginPercent="15%w",?app:layout_marginBottomPercent="20%h".
支持通過app:layout_textSizePercent設(shè)置textView的textSize
對(duì)于外層套ScrollView的問題,目前可以在PercentLinearLayout的外層使用ScrollView,不過對(duì)于寬度的百分比參考的就是android.R.id.content的高度(因?yàn)?#xff0c;無法參考父控件的高度,父控件的高度理論上依賴于子View高度,且模式為UNSPECIFIED)。
對(duì)于如何導(dǎo)入,也是相當(dāng)?shù)暮唵?#xff0c;android studio的用戶,直接:dependencies?{
//...
compile?'com.zhy:percent-support-extends:1.0.1'
}
不需要導(dǎo)入官方的percent-support-lib了。
對(duì)于的三個(gè)類分別為:com.zhy.android.percent.support.PercentLinearLayout
com.zhy.android.percent.support.PercentRelativeLayout
com.zhy.android.percent.support.PercentFrameLayout
對(duì)于eclipse的用戶:github上自行下載源碼,就幾個(gè)類和一個(gè)attrs.xml,也可以在bintray.com/percent-support-extends下載相關(guān)文件。
下面看幾個(gè)具體的示例。
三 具體的示例
Demo 1
xml:<?xml ?version="1.0"?encoding="utf-8"?>
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">
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">
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">
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"
/>
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"/>
Demo 2
xml:<?xml ?version="1.0"?encoding="utf-8"?>
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">
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%"/>
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%"/>
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%"/>
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%"/>
ok,例子都比較簡單,主要就一個(gè)布局文件,可以看出上述我們可以給寬度、高度,邊距等指定參考值為寬度或者高度。這樣的話,在保證圖片寬、高比例、控件設(shè)置為正方形等需求就沒問題了。
接下來還有個(gè)例子,功能主要是設(shè)置TextView對(duì)于textSize的百分比設(shè)置;以及對(duì)于ScrollView的支持。當(dāng)然了,對(duì)于ScrollView的支持,這個(gè)理論上是不支持的,因?yàn)榇蠹叶记宄?#xff0c;如果PercentLinearLayout在ScrollView中,那么高度的模式肯定是UNSPECIFIED,那么理論上來說高度是無限制的,也就是依賴于子View的高度,而百分比布局的高度是依賴于父View的高度的,所有是互斥的。而我們支持是:考慮到編寫代碼的時(shí)候,大多參考的是屏幕高度(android.R.id.content)的高度,所以如果在ScrollView中,編寫10%h,這個(gè)百分比是依賴于屏幕高度的(不包括ActionBar的高度)。
Demo 3
xml:<?xml ?version="1.0"?encoding="utf-8"?>
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">
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">
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%"/>
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%"/>
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%"/>
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%"/>
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%"
/>
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%"
/>
上面的第三個(gè)TextView的字體設(shè)置的就是5%(默認(rèn)參考容器高度)。整個(gè)PercentLinearLayout在ScrollView中。ok~ 姑且這樣,由于源碼比較簡單,大家可以根據(jù)自己的實(shí)際需求去修改,前提盡可能不要改變?cè)械墓δ堋?/p>
四 擴(kuò)展的相關(guān)源碼
(一) 關(guān)于attrs.xml
原庫中所有的屬性的format為fraction,但是由于我期望的寫法有10%w,10%h,10%,沒有找到合適的format,就直接定義為string了~string我可以自己去解析~<?xml ?version="1.0"?encoding="utf-8"?>
(二) 獲取自定義屬性的值及使用
如果看了上篇博文的話,應(yīng)該清楚,對(duì)于自定義屬性的值是在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
*?
*?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方法,在該方法內(nèi)部通過正則校驗(yàn)其合法性,如果合法,則將其拆解封裝成PercentVal對(duì)象,該對(duì)象中記錄百分比值,已經(jīng)知否參考寬度的布爾值(如果參考寬度則為true,否則為false)。對(duì)于沒有后綴w|h的,和原庫的解析方式相同。
PercentVal對(duì)象如下:public?static?class?PercentVal
{
public?float?percent?=?-1;
public?boolean?isBaseWidth;
public?PercentVal(float?percent,?boolean?isBaseWidth)
{
this.percent?=?percent;
this.isBaseWidth?=?isBaseWidth;
}
}
對(duì)于定義的自定義屬性獲取完成之后,剩下的無非是測量時(shí)候?qū)τ谠镜腖ayoutParams中的寬度和高度的賦值做簡單的修改。參考上一篇的源碼,我們直接看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乘以百分比即可(見上代碼注釋),而我們修改的也比較容易,首先判斷參考寬度還是高度,然后乘以百分比(根據(jù)我們的對(duì)象PercentVal的屬性)。
ok,大概的源碼修改就是上述的內(nèi)容,有興趣的可以直接查看源碼。
當(dāng)然了,上述庫中肯定還存在或多或少的問題,大家可以fork完善下,或者直接留言提意見都可以。
github地址:android-percent-support-extend?,用法參考上文,或者README。歡迎star and fork 。
~~have a nice day ~~
ok~
總結(jié)
以上是生活随笔為你收集整理的android百分比扩展枯,Android 增强版百分比布局库 为了适配而扩展的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: UNO板表白电脑电脑如何表白
- 下一篇: 怎么把图片大小变成一样大怎么把一个图片大