如何在 Constraint Layout 上实现重叠/负边距?

159

在ConstraintLayout上实现负边距以实现重叠是否可能? 我想让一张图片居中在布局上,并且有一个文本视图,使其重叠x dp。我尝试了设置负边距值,但没有成功。如果有一种方法可以实现这一点,那就太好了。


1
可能会有所帮助,我还没有尝试过。 - Eugen Pechanec
13个回答

0

可以尝试这种方式,这样会简单得多。

 <?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    tools:context=".MyProfileFragment">

 
    <ImageView

        android:id="@+id/imageViewUserPic"
        android:layout_width="@dimen/dp60"
        android:src="@mipmap/ic_launcher"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        android:layout_margin="20dp"
        android:layout_height="wrap_content">

    </ImageView>


    <ImageView

        android:id="@+id/imageViewEdit"
        app:layout_constraintBottom_toBottomOf="@+id/imageViewUserPic"
        android:src="@drawable/ic_edit_red_round"
        app:layout_constraintEnd_toEndOf="@+id/imageViewUserPic"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">

    </ImageView>



</androidx.constraintlayout.widget.ConstraintLayout>

布局将会是这样的.. 在此输入图片描述

0

这是一个老问题,但仍然经常被问到,最快的方法是通过将顶部和底部限制在您想要锚定的视图的侧面上来实现,就像这样:

        <androidx.appcompat.widget.AppCompatImageView
        android:layout_width="55dp"
        android:layout_height="55dp"
        app:layout_constraintBottom_toBottomOf="@+id/parent_view_id"
        app:layout_constraintTop_toBottomOf="@+id/parent_view_id"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toStartOf="parent" />

这将使其居中于视图的底部行,水平居中对齐。

-2

一种简单的方法。

我不确定最好的方法。

只需使用LinearLayout进行包装即可。

<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
 <View
 android:layout_width="wrap_content"
 android:layout_marginLeft="-20dp"
 android:layout_height="wrap_content"/>
</LinearLayout>

这会增加一些嵌套,但它适用于从顶部动画化视图,而不像其他解决方案在这里假设视图将完全显示。谢谢! - Leo supports Monica Cellio
2
这个答案需要更多的信息。不清楚如何将东西放在LinearLayout中可以用来实现重叠。 - Bob Liberatore

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接