【问题标题】:Android: Repeatedly create such xml Layout in android?Android:在android中反复创建这样的xml Layout?
【发布时间】:2012-02-07 13:30:43
【问题描述】:

我必须创建布局(在 xml 或 java 中),如下图所示:

但应根据条件重复。 假设,如果 Arraylist.size() 大于 2,那么整个布局应该如此重复。 有可能吗?

如果是,请告诉我这是怎么可能的。

我想在线性布局的垂直方向上创建这样的布局。

谢谢。

已编辑: 有了 jin35 的回答,我已经这样做了:

private void doCalculationForMultipleEmployee() {
    singleEmployee.setVisibility(View.GONE);
    for (int i = 0; i<=tempEmployerList.size()-1; i++) {     
        View repeatedLayout = LayoutInflater.from(getApplicationContext()).inflate(R.layout.test);     
        ((TextView)repeatedLayout.findViewById(R.id.list_title)).setText("Employee"+i);     
        // customize repeatedLayout with other data     
        myLinearLayout.addChild(repeatedLayout); 
    }
}

但我在 .inflate 和 .addChild 处遇到语法错误 请帮助我。 这有什么问题?

【问题讨论】:

  • 我看不到图片
  • 例如,您可以使用具有您想要的布局的子元素制作列表视图。
  • @Sver:ListView 不是一个好主意,因为我必须管理许多数据和变量。所以实现起来有点复杂。

标签: android android-layout android-emulator android-ndk


【解决方案1】:

您应该使用 LinearLayout 创建 xml 文件,并使用“重复”布局创建另一个 xml。然后在每个数据部分的代码中使用这个:

for (Employee e : employeeList) {
    View repeatedLayout = LayoutInflater.from(context).inflate(R.your_repeated_layout);
    ((TextView)repeatedLayout.findViewById(R.id.employee_salary)).setText(e.getSalary())
    // customize repeatedLayout with other data
    yourLinearLayout.addChild(repeatedLayout);
}

【讨论】:

  • 好的。因此,如果我有多个员工并且我想显示该员工的所有信息。那么我应该能够用这个想法一一展示给每一位员工吗?意味着 Employee 1 首先是 Employee2 然后是 Employee3.... 并且每个员工的信息应该是不同的。有可能吗?
  • 告诉我一件事,第二名员工显示在第一名员工上方或第一名员工下方。因为如果列表中有两名员工,我想查看第 1 名和第 2 名员工。
  • 您遇到了什么错误?我现在没有 eclipse 来测试代码
  • 我在 .inflate 中遇到错误,例如添加参数以匹配父项。并在 myLinearLayout.addChild(repeatedLayout) 就像将 ass 转换为 LinearLayout。请为此提供帮助。
  • inflate 方法中应该有父级 - 你可以传递你的线性布局,或者 null。
【解决方案2】:

为什么不尝试滚动视图并在其中嵌入图像和文本视图并使用视图方法(布局 onMeasure)链接 onCreate():http://developer.android.com/reference/android/view/View.html

【讨论】:

    猜你喜欢
    • 2011-08-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-22
    • 1970-01-01
    相关资源
    最近更新 更多