繁体   English   中英

如何以编程方式将父视图添加到子视图?

[英]How to add a parent view to a child view programmatically?

我想将HorizontalScrollView作为父视图添加到子视图LinearLayout 有没有办法在Android以编程方式将父视图添加到子视图?

您可以从原始父级中删除子级,将其添加到新父级,然后将新父级添加到原始父级。

就像是:

// Get reference to the child view
val childView = R.findViewById<LinearLayout>(R.id.myChildLinearLayout)

// Create horizontal scroll view (or inflate)
val horizScrollView = HorizontalScrollView(context)

// Remove child view from original parent
removeView(child)

//TODO: setup new layout params on childView as needed

// Add child view to the horizontal scroll view
horizScrollView.addView(childView)

//TODO: setup layout params on horizontal scrollview as needed

// Add horizontal scrollview to the original parent
addView(horizScollView)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM