Is it possible to achieve negative margin on constraint layout to achieve overlap? I am trying to have a image centered on the layout and have a Text view such that it overlaps a by x dp. I tried setting negative margin value but no luck. It would be great if there is a way to achieve this.
相关问题
- How can I create this custom Bottom Navigation on
- Bottom Navigation View gets Shrink Down
- How to make that the snackbar action button be sho
- Listening to outgoing sms not working android
- How to create Circular view on android wear?
相关文章
- android开发 怎么把图片放入drawable的文件夹下
- android上如何获取/storage/emulated/下的文件列表
- androidStudio有个箭头不认识
- SQLite不能创建表
- Windows - Android SDK manager not listing any plat
- Animate Recycler View grid when number of columns
- Why is the app closing suddenly without showing an
- Android OverlayItem.setMarker(): Change the marker
A Simple Way.
I'm not sure best way.
Just wrap using LinearLayout
I found a way to do it much simpler.
Basically have the ImageView, then on the Text View add top constraint to match the top constraint of the image and just add the margin top of the TextView to match to achieve the -ve margin type behavior.
Follow these two issues:
https://code.google.com/p/android/issues/detail?id=212499 https://code.google.com/p/android/issues/detail?id=234866
Update: I want to draw attention to the answer posted by Amir Khorsandi that specifies using
translationY
to inset the view. The answer below works well (thank you for all the votes), but I posted this solution because I believed thattranslationY
would cause problems with visually moving the view but leaving the actual view in the old position. This would cause interaction problems if the view was to respond to touches. (This view of mine was reinforced by goemic who said as much in a comment on Amir Khorsandi's post.)I think that I believed this because this is true with view animation. See Animation in Honeycomb - an old blog entry by relevant.
This is NOT true for the
translationX
andtranslationY
XML attributes.translationX
andtranslationY
will actually move the view. So, the top of the text view can be constrained to the bottom of the image view and thetranslationY
property can be set to-22dp
. This will have the same effect as using theSpace
widget as described below and is easier to implement and understand.If I am missing something, please post a comment here.
Original answer
Although it doesn't appear that negative margins will be supported in
ConstraintLayout
, there is a way to accomplish the effect using the tools that are available and supported. Here is an image where the image title is overlapped22dp
from the bottom of the image - effectively a-22dp
margin:This was accomplished by using a
Space
widget with a bottom margin equal to the offset that you want. TheSpace
widget then has its bottom constrained to the bottom of theImageView
. Now all you need to do is to constrain the top of theTextView
with the image title to the bottom of theSpace
widget. TheTextView
will be positioned at the bottom the theSpace
view ignoring the margin that was set.The following is the XML that accomplishes this effect. I will note that I use
Space
because it is lightweight and intended for this type of use, but I could have used another type ofView
and made it invisible. (You will probably need to make adjustments, though.) You could also define aView
with zero margins and the height of the inset margin you want, and constrain the top of theTextView
to the top of the insetView
.Yet another approach would be to overlay the
TextView
on top of theImageView
by aligning tops/bottoms/lefts/right and make suitable adjustments to margins/padding. The benefit of the approach demonstrated below is that a negative margin can be created without a lot of computation. That is all to say that there are several ways to approach this.Update: For a quick discussion and demo of this technique, see the Google Developers Medium blog post.
Negative Margin for
ConstraintLayout
XMLAnother way is using
translationX
ortranslationY
like this:it will work like
android:layout_marginRight="-25dp"
This is what I figured out after hours of trying to find a solution.
Let us consider two images, image1 and image2. Image2 is to be placed on top of image1 positioned to the bottom-right side.
Overlapping Views Example
We can use Space widget for overlapping views.
Constraint the Space widget's four sides with the four sides of the image1 respectively. For this example, constraint the image2's left side with the Space widget's right side and the image2's top side with the Space widget's bottom side. This will tie image2 with the Space widget and since the Space widget is constrained from all the sides, we can define required horizontal or vertical bias which will move image2 as required.
Additionally, to position image2 on the center-bottom of image1, we can constraint image2's left and right sides with Space widget's left and right sides respectively. Similarly, we can place image2 anywhere by changing image2's constraints with Space widget.