您好,欢迎访问三七文档
当前位置:首页 > 行业资料 > 国内外标准规范 > Android中measure过程、WRAP_CONTENT详解以及xml布局文
在之前一篇博文中Android中View绘制流程以及invalidate()等相关方法分析,简单的阐述了AndroidView制流程的个骤,即:1measure过程---测过程2layout过程---布局过程3draw过程---制过程要想对Android中View块深入理解,对个骤地学是必不可少的,着重讲解如个内容:1measure过程2WRAP_CONTENTMATCH_PARENT/FILL_PARENT属性的原理说明3xml布局文件解析View树的流程分析希望对大家能有帮助--分析版本基于Android2.31WRAP_CONTENTMATCH_PARENT/FILL_PARENT初入Android殿堂的学们,对个属性一定又爱又恨爱的是使用起来挺爽地---照葫芦画瓢即可,恨的是时常混淆几个属性地意义,需要思而行在带着大家重温几个属性的用法(希望没有啰嗦)个属性都用来适应视图的水平或垂直大小,一个以视图的内容或尺基础的布局比精确地指定视图范围更加方便fill_parent设置一个视图的布局fill_parent将强制性地使视图扩展至父元素大小ཱmatch_parentAndroid中match_parent和fill_parent意思一样,但match_parent更贴,于是从2.2开始两个词都可以用,但2.3版本建议使用match_parentིwrap_content自适应大小,强制性地使视图扩展以便显示其全部内容以TextView和ImageView控件例,设置wrap_content将完整显示其内部的文本和图布局元素将根据内容更改大小可不要重复造轮子,以摘自Androidfill_parent、wrap_content和match_parent的区别然,们可以设置View的确宽高,而不是由以属性指定[java]viewplaincopyprint?0.android:layout_weight=wrap_content//自适应大小0.android:layout_weight=match_parent//父视图等高0.android:layout_weight=fill_parent//父视图等高0.android:layout_weight=00dip//精确设置高度值00dip接来,们需要转换视角,看看ViewGroup.LayoutParams类及其派生类2ViewGroup.LayoutParams类及其派生类2.1ViewGroup.LayoutParams类说明AndroidAPI中如绍:LayoutParamsareusedbyviewstotelltheirparentshowtheywanttobelaidout.意思大概是说:View通过LayoutParams类告诉其父视图它想要地大小(即,长度和宽度)因此,个View都包含一个ViewGroup.LayoutParams类或者其派生类,View类依赖于ViewGroup.LayoutParams路径:frameworks\base\core\java\android\view\View.java[java]viewplaincopyprint?0.publicclassViewimplementsDrawable.Callback,KeyEvent.Callback,AccessibilityEventSource{0....0./**0.*Thelayoutparametersassociatedwiththisviewandusedbytheparent0.*{@linkandroid.view.ViewGroup}todeterminehowthisviewshouldbe0.*laidout.0.*{@hide}0.*/0.//该View拥有的LayoutParams属性,父试图添加该View时,会其赋值,特别注意,其类型ViewGroup.LayoutParams0.protectedViewGroup.LayoutParamsmLayoutParams;.....}2.2ViewGroup.LayoutParams源码分析路径位于:frameworks\base\core\java\android\view\ViewGroup.java[java]viewplaincopyprint?无法加载插件无法加载插件0.publicabstractclassViewGroupextendsViewimplementsViewParent,ViewManager{0....0.publicstaticclassLayoutParams{0./**0.*SpecialvaluefortheheightorwidthrequestedbyaView.0.*FILL_PARENTmeansthattheviewwantstobeasbigasitsparent,0.*minustheparent'spadding,ifany.Thisvalueisdeprecated0.*startinginAPILevelandreplacedby{@link#MATCH_PARENT}.0.*/0.@Deprecated.publicstaticfinalintFILL_PARENT=-;//注意值-,Android.版本不建议使用./**.*SpecialvaluefortheheightorwidthrequestedbyaView..*MATCH_PARENTmeansthattheviewwantstobeasbigasitsparent,.*minustheparent'spadding,ifany.IntroducedinAPILevel..*/.publicstaticfinalintMATCH_PARENT=-;//注意值-./**.*SpecialvaluefortheheightorwidthrequestedbyaView.0.*WRAP_CONTENTmeansthattheviewwantstobejustlargeenoughtofit.*itsowninternalcontent,takingitsownpaddingintoaccount..*/.publicstaticfinalintWRAP_CONTENT=-;//注意值-./**.*Informationabouthowwidetheviewwantstobe.Canbeoneofthe.*constantsFILL_PARENTreplacedbyMATCH_PARENT,.*inAPILevelorWRAP_CONTENT.oranexactsize..*/.publicintwidth;//该View的宽度,可以WRAP_CONTENT/MATCH_PARENT或者一个体值0./**.*Informationabouthowtalltheviewwantstobe.Canbeoneofthe.*constantsFILL_PARENTreplacedbyMATCH_PARENT,.*inAPILevelorWRAP_CONTENT.oranexactsize..*/.publicintheight;//该View的高度,可以WRAP_CONTENT/MATCH_PARENT或者一个体值./**.*Usedtoanimatelayouts..*/.publicLayoutAnimationController.AnimationParameterslayoutAnimationParameters;0./**.*Createsanewsetoflayoutparameters.Thevaluesareextractedfrom.*thesuppliedattributessetandcontext.TheXMLattributesmapped.*tothissetoflayoutparametersare:.*/.publicLayoutParamsContextc,AttributeSetattrs{.TypedArraya=c.obtainStyledAttributesattrs,R.styleable.ViewGroup_Layout;.setBaseAttributesa,.R.styleable.ViewGroup_Layout_layout_width,.R.styleable.ViewGroup_Layout_layout_height;0.a.recycle;.}../**.*Createsanewsetoflayoutparameterswiththespecifiedwidth.*andheight..*/.publicLayoutParamsintwidth,intheight{.this.width=width;.this.height=height;无法加载插件0.}./**.*Copyconstructor.Clonesthewidthandheightvaluesofthesource..*.*@paramsourceThelayoutparamstocopyfrom..*/.publicLayoutParamsLayoutParamssource{.this.width=source.width;.this.height=source.height;.}0./**.*UsedinternallybyMarginLayoutParams..*@hide.*/.LayoutParams{.}./**.*Extractsthelayoutparametersfromthesuppliedattributes..*.*@paramathestyleattributestoextracttheparametersfrom0.*@paramwidthAttrtheidentifierofthewidthattribute.*@paramheightAttrtheidentifieroftheheightattribute.*/.protectedvoidsetBaseAttributesTypedArraya,intwidthAttr,intheightAttr{.width=a.getLayoutDimensionwidthAttr,layout_width;.height=a.getLayoutDimensionheightAttr,layout_height;.}.}们发现FILL_PARENT/MATCH_PARENT值-1,WRAP_CONETENT值-2,是不是有点异?将值设置负值的目的是了区别View的体值(anexactsize)总是大于0的ViewGroup子类可以实现自定义LayoutParams,自定义LayoutParams提供了更好地扩展性,例如LinearLayout就有LinearLayout.LayoutParams自定义类(见文)整个LayoutParams类家是挺复杂的ViewGroup.LayoutParams及其常用派生类的类图(部分类图)如:该类图是在庞大了,大家有趣的去看看AndroidAPI前面们说过,个View都包含一个ViewGroup.LayoutParams类或者其派生类,面们的疑问是Android框架中时如何View设置其LayoutParams属性的有两种方法会设置View的LayoutParams属性:1直接添加子View时,常见于如几种方法:ViewGroup.java[java]viewplaincopyprint?0.//Addsachildview.0.voidaddViewViewchild,intindex0.//AddsachildviewwiththisViewGroup'sdefaultlayoutparameters0.//an
本文标题:Android中measure过程、WRAP_CONTENT详解以及xml布局文
链接地址:https://www.777doc.com/doc-1085126 .html