以可用空间为依据,以编程方式设置视图的百分比宽度/高度。

huangapple go评论51阅读模式
英文:

Programmatically set percentage width/height of a view dependent upon available space

问题

我正在尝试使用ConstraintSetconstrainPercentWidthconstrainPercentHeight来根据屏幕宽度或高度的空间较多情况来设置视图的尺寸。等效地,我想使用两者中较小的值作为视图的宽度和高度。伪代码可能看起来像这样:

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 &gt;= 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 &lt;= 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>



huangapple
  • 本文由 发表于 2023年5月25日 11:19:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/76328690.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定