【发布时间】:2021-12-25 23:43:28
【问题描述】:
我有一个字符串列表,我想在线性布局中创建按钮,其文本来自该列表。我在 XML 文件中的线性布局是:
...other stuff...
<LinearLayout
android:id="@+id/my_linear_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"/>
然后在onCreate我有以下代码:
buttons = viewModel.myLiveDataList.value!!.map {
val button = Button(binding.myLinearLayout.context)
button.layoutParams = LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT)
button.text = it.displayName
return button
}
我觉得我做的不对,因为我从来没有在线性布局上调用 addView,这是我见过的所有示例,但是当我尝试调用 addView 时,我收到了关于按钮的错误已经有了父母。
这段代码的结果是片段只显示一个按钮,其文本是列表的第一个条目。它不显示线性布局之前存在的“其他东西”,也不显示列表中任何其他条目的按钮。
我已经通过日志验证了列表本身确实有多个条目,但 map 函数只在第一个条目上执行,我不知道为什么视图中的其他所有内容都被删除了,我只得到一个按钮。
谁能向我解释我做错了什么?
编辑:根据要求,我最初尝试按照示例here 编写以下内容:
buttons = viewModel.myLiveDataList.value!!.map {
val button = Button(this)
button.layoutParams = LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT)
button.text = it.displayName
binding.myLinearLayout.addView(button)
return button
}
这不会编译,因为 Button 需要上下文,所以我将其更改为 Button(this.context),然后它在 addView 上崩溃并显示消息
java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
【问题讨论】:
-
示例here 有帮助吗?您确实需要
addView,因此您可能应该将您的称呼方式添加到问题中 -
您是否考虑过使用
RecyclerView? developer.android.com/guide/topics/ui/layout/recyclerview -
@TylerV:该示例的问题在于,这些类的接口在 Kotlin 中与在 Java 中似乎不同。例如,我不能传入
this来创建一个按钮,我需要传入一个上下文,并且我不能将按钮传递给addView,因为我收到一个关于需要从它的父级中删除它的错误. -
@ChocolateChapta:我没有。如果我无法得到我想要做的工作,那么我想我会针对尝试 #2 进行调查,谢谢。
-
将您尝试的代码添加到问题中。回收者视图是个好主意。
标签: android kotlin android-layout