将多个视图膨胀为片段

时间:2019-01-18 17:51:07

标签: android-layout kotlin layout-inflater android-inflate

我正在开发我的应用程序,我需要列出一小组这样的小视图:

(我无法发布图片,因为StackOverflow是我不出名的原因)

IMAGE

该片段已显示,并且已正确应用,但我尝试显示的视图组未显示。

我找不到如何使片段清晰可见并使按钮正常工作的方法(但这不是重点,因此稍后再问)

class EditFragment : Fragment() {
lateinit var option : Spinner
lateinit var result : TextView
private lateinit var viewOfLayout: View
override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?

): View? {

    viewOfLayout =  inflater.inflate(R.layout.edit_fragment, container, false)

    val mainLayout = viewOfLayout.findViewById(R.id.scrollView1) as ScrollView
    //create a view to inflate the layout_item (the xml with the textView created before)
    val view = layoutInflater.inflate(R.layout.sensor_item, mainLayout, false)

    val options = arrayOf("A","V")

    option = view.spinner as Spinner
    result = view.textView7 as TextView
    option.adapter = ArrayAdapter<String>(activity,android.R.layout.simple_list_item_1,options)

return viewOfLayout
}

}

这是完整的代码:https://pastebin.com/fYyXkupM

(我想我的片段可以正确显示)

->我认为我的错误是从那里来的:

val view = layoutInflater.inflate(R.layout.sensor_item, mainLayout, false)



 如果我将“ mainLayout”替换为“ container”,则可以正常工作,但不是该片段的子级。

谢谢。

2 个答案:

答案 0 :(得分:0)

You haven't initialized option Spinner from viewOfLayout.

答案 1 :(得分:0)

好的,即使这有点具体,我也找到了答案:

val view = layoutInflater.inflate(R.layout.sensor_item, container, false)
val insertPoint = viewOfLayout.findViewById(R.id.box_Parent) as LinearLayout
insertPoint.addView(view2, 0)