myView.getLayoutParams().height = screenHeight 和 myView.setLayoutParams(lp) 之间的区别?
Differences between: myView.getLayoutParams().height = screenHeight and myView.setLayoutParams(lp)?
当谈到动态更改布局的高度时,我通常使用 LayoutParams 设置所需的尺寸,例如:
RelativeLayout myView = (RelativeLayout) v.findViewById(R.id.myView);
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.FILL_PARENT,
screenHeight);
myView.setLayoutParams(lp);
但是还有这个更短的版本:
myView.getLayoutParams().height = screenHeight;
两者都适用于我的情况,我当然更喜欢第二个版本,因为它更简单,但是我需要注意两者之间有什么区别吗?
谢谢!
你可以看看View的source code
调用setLayoutParams
时,也会执行以下语句。
resolveLayoutParams();
if (mParent instanceof ViewGroup) {
((ViewGroup) mParent).onSetLayoutParams(this, params);
}
requestLayout();
所以基本上 requestLayout()
会立即被调用。家长也会被告知这些变化。
使用 myView.getLayoutParams().height = screenHeight;
时,还必须重新布局视图。这可能由 View 本身在其他时间点完成,或者必须手动完成。
这取决于你调用myView.getLayoutParams()的时间.height = screenHeight;
如果 getLayoutParams().height = screenHeight;
在视图第一次 layouted/measured 之前被调用,这应该有效。
当谈到动态更改布局的高度时,我通常使用 LayoutParams 设置所需的尺寸,例如:
RelativeLayout myView = (RelativeLayout) v.findViewById(R.id.myView);
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.FILL_PARENT,
screenHeight);
myView.setLayoutParams(lp);
但是还有这个更短的版本:
myView.getLayoutParams().height = screenHeight;
两者都适用于我的情况,我当然更喜欢第二个版本,因为它更简单,但是我需要注意两者之间有什么区别吗?
谢谢!
你可以看看View的source code
调用setLayoutParams
时,也会执行以下语句。
resolveLayoutParams();
if (mParent instanceof ViewGroup) {
((ViewGroup) mParent).onSetLayoutParams(this, params);
}
requestLayout();
所以基本上 requestLayout()
会立即被调用。家长也会被告知这些变化。
使用 myView.getLayoutParams().height = screenHeight;
时,还必须重新布局视图。这可能由 View 本身在其他时间点完成,或者必须手动完成。
这取决于你调用myView.getLayoutParams()的时间.height = screenHeight;
如果 getLayoutParams().height = screenHeight;
在视图第一次 layouted/measured 之前被调用,这应该有效。