使用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>我的问题是,只要ImageView中图像的高度大于宽度,就不会显示较低的ImageButtons。如果我们在ConstraintLayout上硬编码一个高度(某种足够的高度),那么根据ImageView的高度,按钮可以获得一定的高度。我认为问题是,当View Bounds针对ImageView.进行调整时,我如何克服这种情况?
发布于 2017-06-04 16:55:38
wrap_content仅要求小部件测量自身,但不会根据最终约束限制其扩展
将以下属性添加到您的ImageView中,当parent constraint layout设置为wrap_content时使用该属性,则会出现高度测量问题,因此可以克服
app:layout_constraintHeight_default="wrap"并将ImageView height设置为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"https://stackoverflow.com/questions/44351817
复制相似问题