Create View-Object from XML file in android

2020-06-30 05:06发布

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

3条回答
干净又极端
2楼-- · 2020-06-30 05:37

You should change your line to:

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

You can find it in documentation here.

查看更多
迷人小祖宗
3楼-- · 2020-06-30 05:53
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

查看更多
走好不送
4楼-- · 2020-06-30 05:59

You could make this component invisible with:

android:visibility="gone"

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

查看更多
登录 后发表回答