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

时间:2019-01-25 09:47:56

标签: android android-studio layout android-constraintlayout

这是我的布局:

...
<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)));

3 个答案:

答案 0 :(得分:0)

正确的答案是:

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

答案 1 :(得分:0)

我使用了 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)

...浮点值应为映射到单位间隔的百分比。

答案 2 :(得分:0)

我有同样的问题,幸好找到了这篇文章。我开发了一个虚拟乐器应用程序,一个具有八个主要用户界面(具有圆形主要元素)的钢鼓/平底锅应用程序。我为八个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();
}