获取视图的边距
如何从活动中获取视图的边距值?视图可以是任何类型。
经过一番搜索后,我找到了填充视图的方法,但在 Margin 上找不到任何内容。有人可以帮忙吗?
我尝试了类似的方法,
ViewGroup.LayoutParams vlp = view.getLayoutParams();
int marginBottom = ((LinearLayout.LayoutParams) vlp).bottomMargin;
这有效,但在上面的代码中我假设视图是 LinearLayout。但即使我不知道视图类型,我也需要获取 margin
属性。
How can I get the margin value of a View from an Activity? The View can be of any type.
After a bit of searching I found out ways to get padding of a view, but couldn't find anything on Margin. Can anyone help?
I tried something like this,
ViewGroup.LayoutParams vlp = view.getLayoutParams();
int marginBottom = ((LinearLayout.LayoutParams) vlp).bottomMargin;
This works, but in the above code I have assumed the view to be a LinearLayout
. But I need to get the margin
attribute even when I don't know the view type.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
试试这个:
边距可以通过
编辑: 访问
也许
ViewGroup.MarginLayoutParams
适合您。它是其他 LayoutParams 的基类。http://developer.android.com/reference/android/view/ViewGroup .MarginLayoutParams.html
try this:
margins are accessible via
edit:
perhaps
ViewGroup.MarginLayoutParams
will work for you. It's a base class for otherLayoutParams
.http://developer.android.com/reference/android/view/ViewGroup.MarginLayoutParams.html
尝试
这个至少为我的视图返回了正确的边距。
http://developer.android.com/reference/android/view/ViewGroup.MarginLayoutParams.html< /a>
Try
This returned the correct margains for my view atleast.
http://developer.android.com/reference/android/view/ViewGroup.MarginLayoutParams.html
现在使用这个编辑过的代码。这会帮助你
now use this edited code. this will help you
正如其他人所建议的,layout_margin#是父级的#边缘和您的视图之间的空间。
获取/设置边距对我有用:
当然,我的视图确实是一个 ViewGroup,父级也是一个 ViewGroup。在大多数情况下,您应该将布局参数转换为父级的 View 类 LayoutParams(在本例中为 ViewGroup 和relativelayout)
As others suggested, layout_margin# is the space between the parent's # edge and your view.
Getting/setting margins worked for me with:
Of course, my View was indeed a ViewGroup and the parent was a ViewGroup as well. In most cases, you should cast your layout params to the parent's View class LayoutParams (in this case it's ViewGroup and RelativeLayout)