[英]How to move the setContentView(View view) method to a fragment
I'm using a library ( VTM ) that was designed to be used with activities, and requires the setContentView method passing it a view, like this:我正在使用一个设计用于活动的库( VTM ),并且需要 setContentView 方法将其传递给视图,如下所示:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
MapView mapView = new MapView(this);
setContentView(mapView);
}
I want to move the class to a fragment.我想将 class 移动到片段中。 The MapView constructor does some initialization inside the library and then it needs to be set in the setContentView method, so I can't inflate the layout using the id of an xml layout (which is what the LayoutInflator requires. I want to do something like this:
MapView 构造函数在库中进行了一些初始化,然后需要在 setContentView 方法中进行设置,所以我不能使用 xml 布局的 id 来扩展布局(这是 LayoutInflator 需要的。我想做类似的事情这个:
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(new MapView(requireContext()), container, false);
}
but the inflate method requires the id to a layout xml.但是 inflate 方法需要布局 xml 的 id。 How can I inflate the mapView instead?
我怎样才能给 mapView 充气呢?
Generally, the onCreateView() requires to return a View instance.通常,onCreateView() 需要返回一个 View 实例。 So, you can return your MapView instance itself.
因此,您可以返回 MapView 实例本身。 Hope it works.
希望它有效。
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return new MapView(requireContext());
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.