我试过用
this method
更改RelativeLayout的可见性。这在OnCreateView
函数中运行良好,但我尝试将其添加到另一个函数中:
fun anzeigen(wert: Int){
val inflater = dumpster.infl
val container = dumpster.cont
val view: View = inflater!!.inflate(R.layout.fragment_bots, container, false)
if (wert == 1){
view.BotDiv1.visibility = View.VISIBLE
}
}
为了抓住inflater和容器,我创建了这个对象来存储它们“globaly”:
object dumpster {
var infl: LayoutInflater? = null
var cont: ViewGroup? = null
}
这里是OncreateView:
override fun onCreateView(inflater: LayoutInflater?, container: ViewGroup?, savedInstanceState: Bundle?): View? {
val view: View = inflater!!.inflate(R.layout.fragment_bots, container, false)
dumpster.infl = inflater
dumpster.cont = container
//Test, workes fine view.BotDiv1.visibility = View.VISIBLE
return view
}
现在它不起作用。我记录了所有内容并执行了代码,但RelativeLayout只是保持不可见。 (该功能显然是从外部调用的。) 没有崩溃或任何事情
答案 0 :(得分:1)
在anzeigen
中,您正在为视图充气,但此视图未作为子视图附加到container
。要将新视图附加到容器,您必须致电container.addChild(view)
或使用inflater!!.inflate(R.layout.fragment_bots, container, **true**)