[英]ConstraintLayout inside LinearLayout not working
I have to programmatically add a view (whose root view can be anything) into LinearLayout
but if that view is a ConstraintLayout
, it won't work as expected. 我必须以编程方式将一个视图(其根视图可以是任何东西)添加到LinearLayout
但如果该视图是ConstraintLayout
,它将无法按预期工作。 Why is this happening, because according to my understanding, a child view must work regardless of what its parent view is. 为什么会发生这种情况,因为根据我的理解,无论父视图是什么,子视图都必须工作。 How do I make this work? 我该如何工作?
Problem is very simple and but I am not able to find any question addressing this problem. 问题很简单,但我无法找到解决这个问题的任何问题。
I am attaching screenshots to show the distorted view: 我附上截图来显示扭曲的视图:
ORIGINAL VIEW _____________________ VIEW AFTER ADDING INSIDE LINEAR LAYOUT 原始视图 _____________________ 添加内部线性布局后查看
And here is the code: 以下是代码:
override fun setContentView(view: View?) {
val toolbar = layoutInflater.inflate(R.layout.view_toolbar, null, false)
titleView = toolbar.findViewById(R.id.title) as TextView
val finalView = LinearLayout(this)
finalView.layoutParams = LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT)
finalView.orientation = LinearLayout.VERTICAL
finalView.addView(toolbar)
finalView.addView(view)
super.setContentView(finalView)
}
I am overriding the Activity's setContentView
function. 我重写了Activity的setContentView
函数。
As it turns out, there was nothing wrong with ConstraintLayout
inside LinearLayout
. 事实证明, LinearLayout
ConstraintLayout
没有任何问题。 I just had to explicitly add LayoutParams
while adding my view. 我只需要在添加视图时显式添加LayoutParams
。
override fun setContentView(view: View?) {
val toolbar = layoutInflater.inflate(R.layout.view_toolbar, null, false)
titleView = toolbar.findViewById(R.id.title) as TextView
val finalView = LinearLayout(this)
finalView.layoutParams = LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT)
finalView.orientation = LinearLayout.VERTICAL
finalView.addView(toolbar)
finalView.addView(view, LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT))
super.setContentView(finalView)
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.