获取视图的边距

发布于 2024-12-05 04:27:02 字数 336 浏览 0 评论 0原文

如何从活动中获取视图的边距值?视图可以是任何类型。

经过一番搜索后,我找到了填充视图的方法,但在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

生生漫 2024-12-12 04:27:02

试试这个:

View view = findViewById(...) //or however you need it
LayoutParams lp = (LayoutParams) view.getLayoutParams();

边距可以通过

lp.leftMargin;
lp.rightMargin;
lp.topMargin;
lp.bottomMargin;

编辑: 访问
也许 ViewGroup.MarginLayoutParams 适合您。它是其他 LayoutParams 的基类。

ViewGroup.MarginLayoutParams lp = (ViewGroup.MarginLayoutParams) view.getLayoutParams();

http://developer.android.com/reference/android/view/ViewGroup .MarginLayoutParams.html

try this:

View view = findViewById(...) //or however you need it
LayoutParams lp = (LayoutParams) view.getLayoutParams();

margins are accessible via

lp.leftMargin;
lp.rightMargin;
lp.topMargin;
lp.bottomMargin;

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

浊酒尽余欢 2024-12-12 04:27:02

尝试

ViewGroup.MarginLayoutParams vlp = (MarginLayoutParams) view.getLayoutParams()

vlp.rightMargin
vlp.bottomMargin
vlp.leftMargin
vlp.topMargin

这个至少为我的视图返回了正确的边距。

http://developer.android.com/reference/android/view/ViewGroup.MarginLayoutParams.html< /a>

Try

ViewGroup.MarginLayoutParams vlp = (MarginLayoutParams) view.getLayoutParams()

vlp.rightMargin
vlp.bottomMargin
vlp.leftMargin
vlp.topMargin

This returned the correct margains for my view atleast.

http://developer.android.com/reference/android/view/ViewGroup.MarginLayoutParams.html

一萌ing 2024-12-12 04:27:02

现在使用这个编辑过的代码。这会帮助你

FrameLayout.LayoutParams lp=(FrameLayout.LayoutParams)mainLayout.getLayoutParams();

lp.leftMargin  // for left margin
lp.rightMargin   // for right margin

now use this edited code. this will help you

FrameLayout.LayoutParams lp=(FrameLayout.LayoutParams)mainLayout.getLayoutParams();

lp.leftMargin  // for left margin
lp.rightMargin   // for right margin
一世旳自豪 2024-12-12 04:27:02

正如其他人所建议的,layout_margin#是父级的#边缘和您的视图之间的空间。

  • # 替换“左”、“右”、“上”或“下”

获取/设置边距对我有用:

ViewGroup.MarginLayoutParams params = (ViewGroup.MarginLayoutParams) mView.getLayoutParams();
params.topMargin += 20;
mView.requestLayout();

当然,我的视图确实是一个 ViewGroup,父级也是一个 ViewGroup。在大多数情况下,您应该将布局参数转换为父级的 View 类 LayoutParams(在本例中为 ViewGroup 和relativelayout)

As others suggested, layout_margin# is the space between the parent's # edge and your view.

  • # replaces "Left", "Right", "Top" or "Bottom"

Getting/setting margins worked for me with:

ViewGroup.MarginLayoutParams params = (ViewGroup.MarginLayoutParams) mView.getLayoutParams();
params.topMargin += 20;
mView.requestLayout();

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)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文