获取视图边距 [英] Get margin of a View
问题描述
如何从活动"中获取视图的边距值?视图可以是任何类型.
How can I get the margin value of a View from an Activity? The View can be of any type.
经过一些搜索,我找到了获取视图填充的方法,但是在Margin上找不到任何内容.有人可以帮忙吗?
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?
我尝试过类似的事情
ViewGroup.LayoutParams vlp = view.getLayoutParams();
int marginBottom = ((LinearLayout.LayoutParams) vlp).bottomMargin;
这有效,但是在上面的代码中,我假定视图为LinearLayout
.但是,即使我不知道视图类型,也需要获取margin
属性.
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.
推荐答案
尝试一下:
View view = findViewById(...) //or however you need it
LayoutParams lp = (LayoutParams) view.getLayoutParams();
边距可通过
lp.leftMargin;
lp.rightMargin;
lp.topMargin;
lp.bottomMargin;
修改:
也许ViewGroup.MarginLayoutParams
将为您工作.这是其他LayoutParams
的基类.
edit:
perhaps ViewGroup.MarginLayoutParams
will work for you. It's a base class for other LayoutParams
.
ViewGroup.MarginLayoutParams lp = (ViewGroup.MarginLayoutParams) view.getLayoutParams();
http://developer.android.com/reference/android/view/ViewGroup.MarginLayoutParams.html
这篇关于获取视图边距的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!