Android 将宽度设置为 ConstraintLayout.LayoutParams.MATCH_PARENT 给出 -1
Android setting width to ConstraintLayout.LayoutParams.MATCH_PARENT gives -1
我在创建弹出窗口时有这段代码
val width = ConstraintLayout.LayoutParams.MATCH_PARENT
弹出窗口显示并具有所需的全宽。但由于某些原因,宽度变量被分配为 -1,我使用该变量填充弹出窗口中的其他内容,以便它们获得负宽度并且不显示。为什么当弹出窗口变满时返回 -1?
public static final int MATCH_PARENT Special value for the height or
width requested by a View. MATCH_PARENT means that the view wants to
be as big as its parent, minus the parent's padding, if any.
Introduced in API Level 8.
Constant Value: -1 (0xffffffff) (+)
要获取特定视图的宽度,您可以执行以下操作:
int width = specificView.getWidth();
视图的宽度(猜测 ConstraintLayout
)不是静态的,它取决于呈现视图的设备。
在 kotlin 中,您有一种在运行时计算视图尺寸的便捷方法:
view.doOnLayout {
it.measuredWidth
it.measuredHeight
}
我在创建弹出窗口时有这段代码
val width = ConstraintLayout.LayoutParams.MATCH_PARENT
弹出窗口显示并具有所需的全宽。但由于某些原因,宽度变量被分配为 -1,我使用该变量填充弹出窗口中的其他内容,以便它们获得负宽度并且不显示。为什么当弹出窗口变满时返回 -1?
public static final int MATCH_PARENT Special value for the height or width requested by a View. MATCH_PARENT means that the view wants to be as big as its parent, minus the parent's padding, if any. Introduced in API Level 8.
Constant Value: -1 (0xffffffff) (+)
要获取特定视图的宽度,您可以执行以下操作:
int width = specificView.getWidth();
视图的宽度(猜测 ConstraintLayout
)不是静态的,它取决于呈现视图的设备。
在 kotlin 中,您有一种在运行时计算视图尺寸的便捷方法:
view.doOnLayout {
it.measuredWidth
it.measuredHeight
}