如何从 onCreateView 外部重新膨胀布局

How to re-inflate layout from outside of onCreateView

我被同事完成的 android 项目困住了。现在我没有时间正确地做。 我陷入了必须从 public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) 方法之外重新扩充布局的情况。

有一个从 Fragment 外部调用的 public static void showRecords() 方法,在该方法中我试图通过这样做来膨胀布局:

LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

View view = inflater.inflate(R.layout.formshowlayout, null);

//RelativeLayout item = (RelativeLayout) view.findViewById(R.id.RelativeLayout3);

TextView txtValue = (TextView) view.findViewById(R.id.tvName);
txtValue.setText("some text to show");

我没有遇到任何异常或错误,而且 TextView "some text to show" 也没有显示。

在 android 中可以做我在这里做的事情吗?

您必须指定父级 ViewGroup 并将视图膨胀到其中。

View view = inflater.inflate(R.layout.formshowlayout, container, true);

其中 container 是新视图的分层父视图。