简体   繁体   English

在 Android 中将布局添加为视图

[英]add layout as a view in Android

I have a pretty complex layout defined in xml file , now I want to add this layout as a view using addView or something else.我在xml文件中定义了一个非常复杂的布局,现在我想使用addView或其他东西将此布局添加为视图。

As layout is pretty much complex , its pretty cumbersome process to write the code for layout and then add it as a view.由于布局非常复杂,编写布局代码然后将其添加为视图的过程非常繁琐。 So is there anyway to load layout resource and add it as a view.那么无论如何要加载布局resource并将其添加为视图。

I want to add the layout into WebView我想将布局添加到 WebView

Use利用

LayoutInflater factory = LayoutInflater.from(this);
View myView = factory.inflate(R.layout.my_layout_id, null);

then use addView(myView)然后使用addView(myView)

You can also reduce this to one line of code;您也可以将其简化为一行代码;

  View view = View.inflate(getActivity, R.layout.my_layout,null);

then add to your view.然后添加到您的视图中。

如果您的视图是 ViewGroup(布局)您可以使用InflaterService.inflate(id, ViewGroup)并设置 ViewGroup,它将使用您的 xml 内容设置当前子项。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM