清泛IT社区

标题: 标签的“具有外边距”有啥用? [打印本页]

作者: App Inventor 2     时间: 昨天 22:17
标题: 标签的“具有外边距”有啥用?
https://www.fun123.cn/reference/ ... ml#Label.HasMargins

待研究。

作者: App Inventor 2     时间: 2 小时前
  1. Object lp = view.getLayoutParams();
  2.     // The following instanceof check will fail if we have not previously
  3.     // added the label to the container (Why?)
  4.     if (lp instanceof ViewGroup.MarginLayoutParams) {
  5.       marginLayoutParams = (ViewGroup.MarginLayoutParams) lp;
  6.       defaultLabelMarginInDp = dpToPx(view, DEFAULT_LABEL_MARGIN);
  7.     }
复制代码
根据源码,标签只有在特定的布局中才会有margin,否则就是0不生效。
ViewGroup.MarginLayoutParams 是 Android 布局系统中一个 用于描述视图外边距(margin) 的参数类。它继承自 ViewGroup.LayoutParams,在其基础上新增了对四个方向的 margin 支持。

如何与布局类型配合?
你不能随便用 MarginLayoutParams ——它必须与父容器兼容。常见情况如下:
父容器(ViewGroup)它的 LayoutParams 实际类型是否支持 margin
LinearLayoutLinearLayout.LayoutParams extends MarginLayoutParams✅ 是
RelativeLayoutRelativeLayout.LayoutParams extends MarginLayoutParams✅ 是
ConstraintLayoutConstraintLayout.LayoutParams extends MarginLayoutParams✅ 是
FrameLayoutFrameLayout.LayoutParams extends MarginLayoutParams✅ 是
ViewGroup(纯粹)ViewGroup.LayoutParams❌ 否









欢迎光临 清泛IT社区 (https://bbs.tsingfun.com/) Powered by Discuz! X3.3