我怎么知道我是否需要调用setLayoutParams或只是更改现有LayoutParams的值?

问题描述 投票:0回答:1

我怎么知道getLayotParams()是否返回对有效参数的引用,或者仅仅是参数的深层副本?

说:

MarginLayoutParams layoutParams = (MarginLayoutParams) getView().getLayoutParams();

layoutParams.leftMargin = horizontalMargin;
layoutParams.rightMargin = horizontalMargin;

足够了,还是也需要这一行?

parent.setLayoutParams(layoutParams);
android margin deep-copy android-layoutparams
1个回答
0
投票

getLayoutParams返回实际的LayoutParams,而不是深层副本。但是,更新参数不会触发布局,您必须调用requestLayout,当您使用setLayoutParams时会自动调用。

因此,如果要修改值并应用它们,则需要调用setLayoutParams或使用requestLayout触发布局。

如果使用Kotlin,也可以使用updateLayoutParams扩展功能。

© www.soinside.com 2019 - 2024. All rights reserved.