温馨提示×

Android怎么调用外部xml布局

小亿
98
2023-10-23 21:34:51
栏目: 编程语言

要调用外部的xml布局,可以使用LayoutInflater来动态加载布局文件。具体步骤如下:

  1. 在你想要调用外部xml布局的Activity或Fragment中,创建一个LayoutInflater对象:
LayoutInflater inflater = LayoutInflater.from(context);
  1. 使用LayoutInflater的inflate()方法来加载外部xml布局:
View view = inflater.inflate(R.layout.external_layout, null);

其中,R.layout.external_layout是你想要调用的外部xml布局的资源ID。

  1. 将加载的布局添加到当前的布局中:
ViewGroup rootView = (ViewGroup) findViewById(R.id.root_layout);
rootView.addView(view);

其中,R.id.root_layout是当前布局中的一个容器,你可以根据你的实际情况来替换它。

通过上述步骤,你就可以成功调用外部的xml布局了。

0