首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >安卓- ConstraintLayout以编程方式设定高度百分比?

安卓- ConstraintLayout以编程方式设定高度百分比?
EN

Stack Overflow用户
提问于 2019-01-25 09:47:55
回答 3查看 9K关注 0票数 13

这是我的布局:

代码语言:javascript
复制
...
<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试过了,但没有起作用

代码语言:javascript
复制
ConstraintSet set = new ConstraintSet();
set.constrainPercentHeight(R.id.myclayout, (float) 0.4);
set.applyTo(((ConstraintLayout) vw.findViewById(R.id.myclayout)));
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2019-01-25 15:10:35

正确的答案是:

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

票数 19
EN

Stack Overflow用户

发布于 2020-05-10 21:34:32

我使用ConstraintSet更新了ConstraintLayout的子级的宽度和高度:

代码语言:javascript
复制
        val set = ConstraintSet()
        set.clone(parentLayout) // parentLayout is a ConstraintLayout
        set.constrainPercentWidth(childElement.id, .5f)
        set.constrainPercentHeight(childElement.id, .5f)
        set.applyTo(parentLayout)

...the浮动值应该是映射到单位间隔的百分比。

票数 4
EN

Stack Overflow用户

发布于 2020-09-04 22:59:33

我有同样的问题,谢天谢地找到了这篇文章。我开发了一个虚拟乐器应用程序,一个钢鼓/平底锅应用程序,有八个不同的UI,主要元素是圆形的。我为八个UI中的每一个使用了一个所有屏幕大小的布局,以减少布局冗余,并希望在大多数具有不同屏幕分辨率的设备上保持循环元素的高宽比。因此,我不得不使用layout_constraintWidth_percent和layout_constraintHeight_percent根据设备屏幕纵横比调整父布局的大小。我使用了上面@初学者的代码,并将其与计算屏幕纵横比的代码相结合,从而实现了该思想。以下是代码:

代码语言:javascript
复制
// 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();
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54362659

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档