|
|
从容的饭卡 · データで紡ぐ持続可能な未来。三菱電機の循環型 ...· 4 月前 · |
|
|
善良的番茄 · 王宏志出席第五届国家网络与信息安全信息通报机 ...· 7 月前 · |
|
|
傻傻的凳子 · 爬藤之路——关于教育本质的探讨 | ...· 9 月前 · |
|
|
焦虑的马克杯 · 鋼鐵雄心4 - 萌娘百科 萬物皆可萌的百科全書· 9 月前 · |
|
|
爱旅游的木瓜 · 盐城市退役军人事务局 机关纪检 ...· 1 年前 · |
这是我的布局:
...
<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)));
发布于 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);
发布于 2020-05-10 21:34:32
我使用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)
...the浮动值应该是映射到单位间隔的百分比。
发布于 2020-09-04 22:59:33
我有同样的问题,谢天谢地找到了这篇文章。我开发了一个虚拟乐器应用程序,一个钢鼓/平底锅应用程序,有八个不同的UI,主要元素是圆形的。我为八个UI中的每一个使用了一个所有屏幕大小的布局,以减少布局冗余,并希望在大多数具有不同屏幕分辨率的设备上保持循环元素的高宽比。因此,我不得不使用layout_constraintWidth_percent和layout_constraintHeight_percent根据设备屏幕纵横比调整父布局的大小。我使用了上面@初学者的代码,并将其与计算屏幕纵横比的代码相结合,从而实现了该思想。以下是代码:
// 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);
|
|
焦虑的马克杯 · 鋼鐵雄心4 - 萌娘百科 萬物皆可萌的百科全書 9 月前 |