在ConstraintLayout
中使用约束,我创建了如下布局:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
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="wrap_content"
app:cardCornerRadius="2dp"
app:cardElevation="4dp"
app:cardUseCompatPadding="true">
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="4dp"
>
<ImageView
android:id="@+id/post_photo"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="4dp"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
android:adjustViewBounds="true"
android:scaleType="fitCenter"
app:srcCompat="@drawable/ic_photo"
/>
<ImageButton
android:id="@+id/create_button"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="4dp"
app:layout_constraintTop_toBottomOf="@+id/post_photo"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toLeftOf="@+id/share_button"
app:srcCompat="@drawable/ic_action_share"
android:scaleType="fitCenter"
/>
<ImageButton
android:id="@+id/share_button"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="4dp"
app:layout_constraintTop_toBottomOf="@+id/post_photo"
app:layout_constraintLeft_toRightOf="@+id/create_button"
app:layout_constraintRight_toRightOf="parent"
app:srcCompat="@drawable/ic_action_share"
android:scaleType="fitCenter"
/>
</android.support.constraint.ConstraintLayout>
</android.support.v7.widget.CardView>
我的问题是,只要ImageButtons
中的图像高度超过其宽度,就不会显示较低的ImageView
。如果我们在ConstraintLayout
上对高度(某种程度的足够)进行硬编码,那么按钮可以根据ImageView
的高度获得一些高度。我认为问题是View Bounds
调整为ImageView.
时如何才能克服这种情况?
答案 0 :(得分:1)
wrap_content 仅要求窗口小部件自行测量,但不会限制其扩展以抵御最终约束
将以下属性添加到 ImageView ,当父级约束布局设置为 wrap_content 时使用,然后会出现高度测量问题,以便克服该错误
app:layout_constraintHeight_default="wrap"
并将 ImageView 高度设置为0dp
<ImageView
android:id="@+id/post_photo"
android:layout_width="0dp"
app:layout_constraintHeight_default="wrap"
android:layout_height="0dp"
android:layout_marginTop="4dp"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
android:adjustViewBounds="true"
android:scaleType="fitCenter"
app:srcCompat="@drawable/ic_photo"
/>
信息相同的方法我们可以在宽度问题中使用 只需将width替换为0dp并跟随属性
app:layout_constraintWidth_default="wrap"
答案 1 :(得分:0)
如果您想阻止这种情况,您需要定义百分比或(dp)值。
1 - 您可以在ConstraintLayout上使用 maxWidth / maxHeight ,例如
<ImageView
...
android:maxHeight="192dp"
/>
2 - 您也可以使用指南,如
<Guideline
id="guideLineMaxHeightOfImage"
guideLinePercent="0.4"
orientation="horizontal"
>
<强> ImageView的强>
<ImageView
...
app:layout_constraintTop_toTopOf="@+id/guideLineMaxHeightOfImage"
/>