当前位置: 代码迷 >> java >> 将xml添加到android studio中的LinearLayout项
  详细解决方案

将xml添加到android studio中的LinearLayout项

热度:105   发布时间:2023-07-17 20:59:01.0

我有xml布局,其中包括头像图片??的名称和文本框。 这些都在一个xml文件中。 我想将这些程序实例添加到嵌套在scoll视图中的线性布局。 这是我的代码。

View v = getLayoutInflater().inflate(R.layout.include_message, null);
    LinearLayout stallWall = (LinearLayout) v.findViewById(R.id.stallMessages);

R.layout.include_message =我的xml R.id.stallMessages =线性布局

我没有错误,我看到没有添加任何项目。 我想读取一个数组并为每条消息添加一个include_message。

首先,我不建议使用x = y = z = a等等。 它可能有效,但阅读和理解并不容易。 正如codeMagic所说,你应该使用addView()方法来实现这一点。

这是一个示例

如果您尝试使用xml文件中定义的布局,可以像这样使用LayoutInflater:

View view = getLayoutInflater().inflate(R.layout.yourfile, null);
layout.addView(view);
  相关解决方案