Create View-Object from XML file in android

十年热恋 提交于 2019-12-05 09:34:12

问题


I only want to get an object from a xml layout file without having to implement it into the current layout.

I know the way with

LayoutInflater.from(context).inflate(R.layout.myfile, myparent, true);

but after execution of the above the layout will be implemented and shown immediately inside my "myparent"-View, right? I only want to get the Object itself to get its attributes and such. And maybe (but only maybe) insert it later into the shown layout. Is that possible?

Regards


回答1:


You should change your line to:

LayoutInflater.from(context).inflate(R.layout.myfile, null);

You can find it in documentation here.




回答2:


LayoutInflater.from(context).inflate(R.layout.myfile, myparent, true);

The end parameter determines whether or not to automatically add the new view to myparent. Turn it to false to still use the parent's layout attributes.

Or, if you don't care about the parent's layout params, follow @inazaruk's answer




回答3:


You could make this component invisible with:

android:visibility="gone"

Source: http://developer.android.com/reference/android/view/View.html#attr_android:visibility



来源:https://stackoverflow.com/questions/8481329/create-view-object-from-xml-file-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!