imageview 的 marginRight 属性不起作用
我有一个图像视图,其背景是可绘制的 xml 形状,即可绘制的矩形形状。我的应用程序的方向固定为横向视图。
imageview是相对布局的。
我试图通过设置layout_marginRight的适当值将其移动到屏幕右侧,但这不起作用。imageView始终保持在其原始位置。
我也尝试过以下其他选项,但没有任何帮助。
我尝试过的其他选项是:
- 创建新的相对布局参数并设置右侧边距
- 创建新的边距布局参数并设置位置
- 尝试填充选项
- 将图像视图设置为相对于另一个图像视图的正确位置...
- 使用显示指标来获取宽度屏幕并相应地设置边距......
自从一周以来我一直在设置此图像视图的位置......
我认为最好的方法是将此图像视图设置在两个图像视图之间,因为我无法移动它设置边距但这也不起作用...
这是main.xml 中我的 imageview 的当前 xml:-
<ImageView
android:id="@+id/rect1"
android:background="@drawable/rect"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginRight="70dp"
android:scaleType="fitCenter"/>
I have an image view whose background is a xml shape drawable i.e. a rectangle shape drawable.My app's orientation is fixed to landscape view.
The imageview is in relative layout.
I am trying to move it to the right of the screen by setting the appropriate value of layout_marginRight but this does not work .The imageView always stays in its's original position.
I have tried the following other options also but none helped.
The other options which I tried are:
- Creating a new relative layout params and setting the right margin
- Creating new margin layout params and setting the position
- Trying padding option
- Setting the imageview to right position relative to another imageview...
- Using display metrics to get width of screen and accordingly setting the margin....
I am stuck since a week setting the position of this imageview...
I was thinking the best approach is to set this imageview in between two imageview as I am not able to move it by setting margin but that does not work either...
Here is the current xml of my imageview in main.xml:-
<ImageView
android:id="@+id/rect1"
android:background="@drawable/rect"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginRight="70dp"
android:scaleType="fitCenter"/>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
将RelativeLayout 的layout_width 属性设置为fill_parent 并查看它的工作情况:)
Make the layout_width attribute of the RelativeLayout to fill_parent and see it work :)
我的问题是我的父布局的宽度设置为
wrap_content
,我认为它在编辑器期间没有定义宽度。因此,我无法使用marginRight
来获取偏移量。我必须将宽度更改为固定大小,例如match_parent
或恒定的 dp 大小。My issue was that my parent layout's width was set to
wrap_content
, which doesn't have a defined width during the editor, I think. Thus, I couldn't usemarginRight
to get an offset. I had to change the width to a fixed size likematch_parent
or a constant dp size.将图像视图放入线性布局中,然后使用 android:layout_marginRight="70dp
put the image view in a linearlayout, then use android:layout_marginRight="70dp
我不明白为什么在图像右侧添加边距将有助于将图像向右移动,它只是通过边距扩展了图像视图的右侧边界。只要图像视图实际上不在父视图的右侧,就允许它在不改变位置的情况下增长。如果您希望它位于相对布局的右侧,我建议在 imageview 上使用
layout_alignParentRight="true"
,然后您可以使用 marginRight 来控制您想要的右侧的距离。I don't see why adding a margin to the right of the image would help move the image to the right, it just extends the right side bounds of the imageview by the margin. As long as the imageview isn't actually on the right side of the parentview, it would be allowed to grow without changing position. I suggest using
layout_alignParentRight="true"
on the imageview if you want it on the right of your relativelayout, and then you can use the marginRight to control how far off the right side you want it.