Android - 在布局边缘居中使用ImageView

时间:2017-09-23 15:29:08

标签: android android-imageview android-constraintlayout

我正在使用ConstraintLayout,我想将ImageView放在布局边缘,如下图所示: imageview centered

如何在不使用高程的情况下使用ConstraintLayout来实现这一目标(我不知道如何处理前Lollipop设备中的高程)。

到目前为止,这是我的代码:

 <?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:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="300dp"
    app:behavior_hideable="false"
    app:behavior_peekHeight="190dp"
    android:clickable="true"
    android:focusable="true"
    android:background="#eee"
    app:layout_behavior="android.support.design.widget.BottomSheetBehavior"
    android:id="@+id/main_ride_finished_container"
    >


    <de.hdodenhof.circleimageview.CircleImageView
        android:id="@+id/main_driver_enroute_BS_driverImage"
        android:layout_width="70dp"
        android:layout_height="70dp"
        android:src="@color/colorPrimary"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        android:elevation="3dp"
        />
    <android.support.constraint.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="35dp"
        android:background="#FFFFFF"
        >


    </android.support.constraint.ConstraintLayout>


</android.support.constraint.ConstraintLayout>

1 个答案:

答案 0 :(得分:2)

您可以使用适当的约束放置以布局边缘为中心的视图。在下面的简单示例中,ImageView被约束到封闭容器的开头和结尾,以使其水平居中。图像视图的顶部和底部被约束到内部布局的底部以居中于底部。这就是ConstraintLayout处理&#34;不可能&#34;限制。请参阅doc

enter image description here

<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.ConstraintLayout
        android:id="@+id/innerLayout"
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:background="@color/colorPrimary" />

    <ImageView
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:background="@color/colorAccent"
        app:layout_constraintBottom_toBottomOf="@id/innerLayout"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/innerLayout" />

</android.support.constraint.ConstraintLayout>