微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

inflate用一个XML源填充view. LayoutInflater


java.lang.Object
android.view.LayoutInflater
ThisclassisusedtoinstantiatelayoutXMLfileintoitscorrespondingViewobjects.
Itisneverbeuseddirectly--
usegetLayoutInflater()orgetSystemService(String)toretrieveastandardLayoutInflaterinstance
thatisalreadyhookeduptothecurrentcontextandcorrectlyconfiguredforthedeviceyouarerunningon.
Forexample:

LayoutInflaterinflater=(LayoutInflater)context.getSystemService
Context.LAYOUT_INFLATER_SERVICE);
常用方法有:
publicViewinflate(intresource,ViewGrouproot)
Since:APILevel1

Inflateanewviewhierarchyfromthespecifiedxmlresource.ThrowsInflateExceptionifthereisanerror.
Parameters
resourceIDforanXMLlayoutresourcetoload(e.g.,R.layout.main_page)
rootOptionalviewtobetheparentofthegeneratedhierarchy.
Returns

*TherootViewoftheinflatedhierarchy.Ifrootwassupplied,thisistherootView;otherwiseitistherootoftheinflatedXMLfile.
注意:如果root被提供的话,在把新生成的View连接到root后,返回root.否者返回的是新生成的View
publicViewinflate(intresource,ViewGrouproot,booleanattachToRoot) Since:APILevel1 Inflateanewviewhierarchyfromthespecifiedxmlresource.ThrowsInflateExceptionifthereisanerror. Parameters resourceIDforanXMLlayoutresourcetoload(e.g.,R.layout.main_page) rootOptionalviewtobetheparentofthegeneratedhierarchy(ifattachToRootistrue),orelsesimplyanobjectthatprovidesasetofLayoutParamsvaluesforrootofthereturnedhierarchy(ifattachToRootisfalse.) attachToRootWhethertheinflatedhierarchyshouldbeattachedtotherootparameter?Iffalse,rootisonlyusedtocreatethecorrectsubclassofLayoutParamsfortherootviewintheXML. Returns *TherootViewoftheinflatedhierarchy.IfrootwassuppliedandattachToRootistrue,thisisroot;otherwiseitistherootoftheinflatedXMLfile. 注意:如果root被提供而且attachToRoot为TRUE的话,在把新生成的View连接到root,返回root.否者返回的是新生成的View。 如果root被提供但attachToRoot为FALSE的话,root只是把它的LayoutParams参数给新生成的View用, 不会把新生成的View连接到root。当然返回的是新生成的View。

原文地址:https://www.jb51.cc/xml/300005.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。