设置边距将视图右移到容器外

问题描述 投票:0回答:1
  • 我正在尝试为FrameLayout大小为wrap_content的视图设置边距。

  • 当我增加底部边距时,它可以很好地提高容器的高度,并且视图始终停留在容器中。

  • 但是当我向右增加边距时,容器的宽度会增加,但是视图会在容器外部缓慢移动。

  • 我已经尝试将clipToPaddingclipChildren设置为true(默认值),

这是用于设置边距的代码:

         // inside a FrameLayout in a Click Listener, seem to work for bottom, but right make the view moving outside
           (theView.layoutParams as LayoutParams).bottomMargin += 5
           (theView.layoutParams as LayoutParams).rightMargin += 5
           theView.requestLayout()

编辑:这是xml的简历,在扩展FrameLayout的类中被夸大,]]

<merge>

<CustomView
        android:id="@+id/viewWhoMoveWithMargin"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|center_horizontal" />

<CustomView .. (with same params) />
<CustomView .. (with same params) />
<CustomView .. (with same params) />
<ImageView .. (with same params, this one is the button to increase the margin) />

</merge>

要重现此行为,只需在FrameLayout中添加一个大小为wrap_content的ImageView(img1),将其放入主布局中(带有match_parent),然后添加一个按钮即可更改ImageView(img1)的边距在包装的内容FrameLayout中,

当img1的边距增加时,被包装的内容FrameLayout应该增加,

这里是显示发生了什么的视频:

https://streamable.com/z1tbf

编辑2:我刚刚通过用LinearLayout替换FrameLayout进行了测试,它似乎起作用了,FrameLayout怎么了?

我正在尝试在具有wrap_content大小的FrameLayout中将边距设置为View。当我增加底部边距时,在容器高度增加时效果很好,并且视图始终停留在容器中。 ...

android android-framelayout android-viewgroup
1个回答
0
投票

正在删除android:layout_gravity="bottom|center_horizontal"

© www.soinside.com 2019 - 2024. All rights reserved.