Android - 在布局边缘居中 ImageView

标签 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>

最佳答案

您可以通过使用适当的约束将 View 放置在布局边缘的中心。在下面的简单示例中,ImageView 被限制在封闭容器的开始和结束位置,以使其水平居中。 ImageView 的顶部和底部被限制在内部布局的底部以底部为中心。这就是 ConstraintLayout 处理“不可能”约束的方式。查看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>

关于Android - 在布局边缘居中 ImageView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46381218/

相关文章:

android - 在主要 Activity 的选项卡顶部拉伸(stretch)的 fragment 约束布局

java - 不幸的是 MyApp 已停止。我该如何解决这个问题?

android - 以编程方式将ImageView的重力设置为android中的中心

android - 使用 Switch 在 Android 上无限期振动

android - 动画时比屏幕大的图像被裁剪

android - 如何允许用户在android中设置图像

android - 不同设备的布局 - 约束布局和最小宽度限定符

android - 约束布局在 Android Studio 2.3 中的默认模板中给出错误

android - 如何为 Android 构建 SDL 库

android - 对话 fragment : Using AlertDialog with custom layout