英文:
Programmatically set percentage width/height of a view dependent upon available space
问题
我正在尝试使用ConstraintSet
的constrainPercentWidth
或constrainPercentHeight
来根据屏幕宽度或高度的空间较多情况来设置视图的尺寸。等效地,我想使用两者中较小的值作为视图的宽度和高度。伪代码可能看起来像这样:
val cs = ConstraintSet()
//...
if (widthIsSmaller)
cs.constrainPercentWidth(viewId, percent)
else
cs.constrainPercentHeight(viewId, percent)
cs.setDimensionRatio(viewId, "1:1")
如果有人能指导我正确的方向,我将不胜感激!
英文:
I am trying to use ConstraintSet
's constrainPercentWidth
or constrainPercentHeight
to set the dimensions of a view dependent upon whether the screen has more space width-wise or height-wise. Equivalently, I'd like to use the smaller of the two values as the width and height for the view. The pseudocode might look something like this.
val cs = ConstraintSet()
//...
if (widthIsSmaller)
cs.constrainPercentWidth(viewId, percent)
else
cs.constrainPercentHeight(viewId, percent)
cs.setDimensionRatio(viewId, "1:1")
If anyone could point me in the right direction, I'd really appreciate it!
答案1
得分: 0
你可以在主活动的onCreate中使用这段代码来获取视图的高度和宽度(以像素为单位)。
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
WindowMetrics windowMetrics = activity.getWindowManager().getCurrentWindowMetrics();
Insets insets = windowMetrics.getWindowInsets()
.getInsetsIgnoringVisibility(WindowInsets.Type.systemBars());
width = windowMetrics.getBounds().width() - insets.left - insets.right;
height = windowMetrics.getBounds().height() - insets.top - insets.bottom;
} else {
DisplayMetrics displayMetrics = new DisplayMetrics();
activity.getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
width = displayMetrics.widthPixels;
height = displayMetrics.heightPixels;
}
然后,将约束设置为:
val percentage = 0.5 // 它将占用宽度或高度的一半
val smallerDimension = minOf(width, height)
val desiredDimension = (smallerDimension * percentage).toInt()
if (width <= height) {
constraintSet.constrainPercentWidth(viewId, (desiredDimension.toFloat() / screenWidth))
constraintSet.constrainPercentHeight(viewId, 1.0f)
} else {
constraintSet.constrainPercentWidth(viewId, 1.0f)
constraintSet.constrainPercentHeight(viewId, (desiredDimension.toFloat() / screenHeight))
}
英文:
You can use this to get the height and view in pixels in the main activity's onCreate.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
WindowMetrics windowMetrics = activity.getWindowManager().getCurrentWindowMetrics();
Insets insets = windowMetrics.getWindowInsets()
.getInsetsIgnoringVisibility(WindowInsets.Type.systemBars());
width = windowMetrics.getBounds().width() - insets.left - insets.right;
height = windowMetrics.getBounds().height()-insets.top-insets.bottom;
} else {
DisplayMetrics displayMetrics = new DisplayMetrics();
activity.getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
width= displayMetrics.widthPixels;
height = displayMetrics.heightPixels;
}
And then set the contrains as
val percentage = 0.5 // it would take half of the width or height
val smallerDimension = minOf(width,height)
val desiredDimension = (smallerDimension * percentage).toInt()
if ( width <= height) {
constraintSet.constrainPercentWidth(viewId, (desiredDimension.toFloat() / screenWidth))
constraintSet.constrainPercentHeight(viewId, 1.0f)
} else {
constraintSet.constrainPercentWidth(viewId, 1.0f)
constraintSet.constrainPercentHeight(viewId, (desiredDimension.toFloat() / screenHeight))
}
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论