在android中从XML文件创建View-Object

Mat*_*bon 22 android android-layout layout-inflater

我只想从xml布局文件中获取一个对象,而不必将其实现到当前布局中.

我知道的方式

LayoutInflater.from(context).inflate(R.layout.myfile, myparent, true);
Run Code Online (Sandbox Code Playgroud)

但执行完上面后,布局将实现并立即显示在我的"myparent"-View中,对吗?我只想让Object本身获得它的属性等.也许(但只是可能)稍后将其插入到所示的布局中.那可能吗?

问候

ina*_*ruk 39

您应该将您的行更改为:

LayoutInflater.from(context).inflate(R.layout.myfile, null);
Run Code Online (Sandbox Code Playgroud)

你可以在这里的文档中找到它.


Fun*_*onk 5

LayoutInflater.from(context).inflate(R.layout.myfile, myparent, true);
Run Code Online (Sandbox Code Playgroud)

end 参数决定是否自动将新视图添加到 myparent。将其设置为 false 以仍使用父级的布局属性。

或者,如果您不关心父母的布局参数,请按照@inazaruk 的回答进行操作