Android 为什么ConstraintLayout 1.1.0 layout_constraintWidth_percent和layout_constraintDimensionRatio不能一起工作?
我试图在ImageView中同时使用layout_constraintDimensionRatio和layout_constraintWidth_percent属性,但我发现它们不能一起工作 我的xml代码是: 1.仅使用布局约束宽度百分比:Android 为什么ConstraintLayout 1.1.0 layout_constraintWidth_percent和layout_constraintDimensionRatio不能一起工作?,android,android-layout,android-constraintlayout,Android,Android Layout,Android Constraintlayout,我试图在ImageView中同时使用layout_constraintDimensionRatio和layout_constraintWidth_percent属性,但我发现它们不能一起工作 我的xml代码是: 1.仅使用布局约束宽度百分比: 2.使用布局约束宽度百分比和布局约束尺寸比率: 很明显,当我使用layout_constraintDimensionRatio时,layout_constraintWidth_percent不起作用 我真正想要的是ImageView的宽度是其父视图
2.使用布局约束宽度百分比和布局约束尺寸比率:
很明显,当我使用layout_constraintDimensionRatio时,layout_constraintWidth_percent不起作用
我真正想要的是ImageView的宽度是其父视图的50%,然后使用layout\u constraintWidth\u percent和layout\u constraintDimensionRatio将ImageView的高度设置为其宽度的50%。设置android:layout\u height=“0dp”
以匹配约束将有助于实现所需的结果
根据文件:
如果两个尺寸都设置为匹配_约束(0dp),也可以使用比率。在这种情况下,系统设置满足所有约束的最大尺寸,并保持指定的纵横比
及
你的方法确实有效。还有一个问题,为什么要将app:layout\u constraint-dimensionratio=“w,2:1”更改为app:layout\u constraint-dimensionratio=“2:1”?嗯,当我将app:layout\u constraint-dimensionratio=“2:1”更改为app:layou\u constraint-dimensionratio=“3:2”时,会发生一些事情?我不知道怎么贴这张照片。我真丢脸。你能帮我一下吗?我意识到它有一些问题,并做了更多的测试。我更新了我的答案。你的答案很有道理。
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:scaleType="fitXY"
app:layout_constraintWidth_percent="0.5"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@mipmap/ic_launcher"/>
</android.support.constraint.ConstraintLayout>
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintDimensionRatio="w,2:1"
android:scaleType="fitXY"
app:layout_constraintWidth_percent="0.5"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@mipmap/ic_launcher"/>
</android.support.constraint.ConstraintLayout>
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="0dp"
android:layout_height="0dp"
android:scaleType="fitXY"
app:layout_constraintWidth_percent="0.5"
app:layout_constraintDimensionRatio="2:1"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@mipmap/ic_launcher"/>
</android.support.constraint.ConstraintLayout>
<ImageView
android:layout_width="0dp"
android:layout_height="0dp"
android:scaleType="fitXY"
app:layout_constraintDimensionRatio="H,2:1"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintWidth_percent="0.5"
app:srcCompat="@mipmap/ic_launcher" />
<ImageView
android:layout_width="0dp"
android:layout_height="0dp"
android:scaleType="fitXY"
app:layout_constraintDimensionRatio="H,2:1"
app:layout_constraintHorizontal_bias="0"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintWidth_percent="0.5"
app:srcCompat="@mipmap/ic_launcher" />
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.constraint.Guideline
android:id="@+id/guideline"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_percent="0.5" />
<ImageView
android:layout_width="0dp"
android:layout_height="0dp"
android:scaleType="fitXY"
app:layout_constraintDimensionRatio="H,2:1"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toLeftOf="@id/guideline"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintWidth_percent="0.5"
app:srcCompat="@mipmap/ic_launcher" />
</android.support.constraint.ConstraintLayout>