【发布时间】:2018-06-23 06:43:52
【问题描述】:
我试图在 ImageView 中同时使用 layout_constraintDimensionRatio 和 layout_constraintWidth_percent 属性,但我发现它们不能一起工作。
我的xml代码是:
1.仅使用 layout_constraintWidth_percent:
<?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>
其对应的UI是: layout_constraintWidth_percent_only.png
2.使用 layout_constraintWidth_percent 和 layout_constraintDimensionRatio:
<?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>
其对应的UI是: layout_constraintWidth_percent_and_layout_constraintDimensionRatio.png
很明显,当我使用 layout_constraintDimensionRatio 时,layout_constraintWidth_percent 不起作用。
我真正想要的是 ImageView 的宽度是其父级的 50%,然后通过将 layout_constraintWidth_percent 和 layout_constraintDimensionRatio 结合使用,ImageView 的高度是其宽度的 50%。
【问题讨论】:
标签: android android-layout android-constraintlayout