Android - 如何以编程方式设置ConstraintLayout的百分比高度?

15

这是我的布局:

...
<android.support.constraint.ConstraintLayout   
android:layout_width="0dp"
android:layout_height="match_parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintWidth_percent="0.6"
>
            <android.support.constraint.ConstraintLayout
                android:id="@+id/myclayout"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                app:layout_constraintHeight_percent="0.2"
                app:layout_constraintTop_toTopOf="parent"
                ></android.support.constraint.ConstraintLayout>


 </android.support.constraint.ConstraintLayout>
...

如何通过编程方式设置constraintHeight_percent?

我尝试使用ConstraintSet,但没有成功。

ConstraintSet set = new ConstraintSet();
set.constrainPercentHeight(R.id.myclayout, (float) 0.4);
set.applyTo(((ConstraintLayout) vw.findViewById(R.id.myclayout)));

请查看我的这个答案 https://dev59.com/_bHma4cB1Zd3GeqPHi0n#54230893,你可以从中得到参考。 - Jeel Vankhede
就算价值不高,ConstraintLayout 的一个主要优点是避免嵌套;看到它们相互嵌套是一个警示信号。谢谢。 - Tom Howard
3个回答

24

正确答案是:

    ConstraintLayout mConstrainLayout  = (ConstraintLayout) vw.findViewById(R.id.myclayout);
    ConstraintLayout.LayoutParams lp = (ConstraintLayout.LayoutParams) mConstrainLayout.getLayoutParams();
    lp.matchConstraintPercentHeight = (float) 0.4;
    mConstrainLayout.setLayoutParams(lp);

5

我选择使用ConstraintSet来更新ConstraintLayout子视图的宽度和高度:

        val set = ConstraintSet()
        set.clone(parentLayout) // parentLayout is a ConstraintLayout
        set.constrainPercentWidth(childElement.id, .5f)
        set.constrainPercentHeight(childElement.id, .5f)
        set.applyTo(parentLayout)

浮点数值应该是映射到单位区间的百分比。


0

我曾经有同样的问题,幸运的是找到了这篇文章。我开发了一个虚拟音乐器应用程序,一个带有八个不同UI的钢鼓/平底锅应用程序,具有圆形主要元素。我为每个八个UI使用了一个布局来减少布局冗余,并希望在大多数具有不同屏幕分辨率的设备上保持圆形元素的纵横比。因此,我必须使用layout_constraintWidth_percent和layout_constraintHeight_percent来根据设备屏幕纵横比调整父布局的大小。我使用了@beginner上面的代码,并结合计算屏幕纵横比的代码和随后实现的想法。以下是代码:

// instantiate constraint layout inside OnCreate
ConstraintLayout mcLayout =findViewById(R.id.layout_myConstraintLayout);
    
//determine display aspect ratio
WindowManager wm = (WindowManager) 
this.getSystemService(Context.WINDOW_SERVICE);
assert wm != null;
Display display = wm.getDefaultDisplay();
DisplayMetrics metrics = new DisplayMetrics();
assert display != null;
display.getMetrics(metrics);
float width = metrics.widthPixels;
float height = metrics.heightPixels;
float ratio = width/height;
float aspectRatio = round(ratio, 2);
Log.d("DISPLAYRATIO", "Display Ration is: " + displayRatio);

// create contraintlayout set for mConstraintLayout and params
ConstraintSet set = new ConstraintSet();
ConstraintLayout.LayoutParams lp = (ConstraintLayout.LayoutParams) 
lowTenorBkg.getLayoutParams();

// set percent width and height acacording to screen display aspect ratio
if (aspectRatio > 1.30f && aspectRatio < 1.42f) {
    // Add constraints
    lp.matchConstraintPercentWidth = 0.96f;
    lp.matchConstraintPercentHeight = 0.78f;
} else if (aspectRatio > 1.40f && aspectRatio < 1.60f) {
    // Add constraints
    lp.matchConstraintPercentWidth = 0.96f;
    lp.matchConstraintPercentHeight = 0.83f;
} else if (aspectRatio > 1.58f && aspectRatio < 1.67f) {
    // Add constraints
    lp.matchConstraintPercentWidth = 0.96f;
    lp.matchConstraintPercentHeight = 0.93f;
} else if (aspectRatio > 1.65f && aspectRatio <= 1.71f) {
    // Add constraints
    lp.matchConstraintPercentWidth = 0.92f;
    lp.matchConstraintPercentHeight = 0.94f;
} else if (aspectRatio > 1.72f && aspectRatio <= 1.78f) {
    // Add constraints
    lp.matchConstraintPercentWidth = 0.875f;
    lp.matchConstraintPercentHeight = 0.94f;
} else if (aspectRatio > 1.78f && aspectRatio <= 2.05f) {
    // Add constraints
    lp.matchConstraintPercentWidth = 0.8f;
    lp.matchConstraintPercentHeight = 0.93f;
}

lowTenorBkg.setLayoutParams(lp);
set.applyTo(mcLayout);

// put the round method inside the activity class 
// method for rounding float number to decimal places
public static float round(float d, int decimalPlace) {
   return BigDecimal.valueOf(d).setScale(decimalPlace,
   BigDecimal.ROUND_HALF_UP).floatValue();
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接