区别在于:myView.getLayoutParams()高度= screenHeight和myView.setLayoutParams(LP)? [英] Differences between: myView.getLayoutParams().height = screenHeight and myView.setLayoutParams(lp)?

查看:199
本文介绍了区别在于:myView.getLayoutParams()高度= screenHeight和myView.setLayoutParams(LP)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当涉及到动态改变布局的高度,我通常设置使用的LayoutParams像期望的尺寸:

When it comes to change the height of a layout dynamically, I usually set the desired dimensions using LayoutParams like:

RelativeLayout myView = (RelativeLayout) v.findViewById(R.id.myView);     
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
                        RelativeLayout.LayoutParams.FILL_PARENT,
                        screenHeight);

    myView.setLayoutParams(lp);

但是,这个较短的版本,以及:

But there is this shorter version as well:

myView.getLayoutParams().height = screenHeight;

两者都在我的案子,我会preFER课程的第二个版本,因为要简单得多,但有两个我需要知道什么区别?

Both are working in my case, I would prefer the second version of course because is much simpler, but is there any difference between the two I need to be aware of?

谢谢!

推荐答案

您可以一看的来源$ C查看的$ C

致电时 setLayoutParams 下面的语句也执行。

When calling setLayoutParams the following statements are also executed.

resolveLayoutParams();
if (mParent instanceof ViewGroup) {
    ((ViewGroup) mParent).onSetLayoutParams(this, params);
}
requestLayout();

所以基本上 requestLayout()立即调用。另外,父母被告知的变化。

So basically the requestLayout() is called immediately. Also the parent is informed about the changes.

在使用 myView.getLayoutParams()高度= screenHeight; 查看,还必须relayouted。这可能是由在视图本身在一个其它点来完成或已被手动完成。

When using myView.getLayoutParams().height = screenHeight; the View also must be relayouted. This might be done by the View itself at an other point or has to be done manually.

这取决于当你调用myView.getLayoutParams时间()高度= screenHeight;

This depends on the time when you call myView.getLayoutParams().height = screenHeight;

如果 getLayoutParams()高度= screenHeight; 查看之前被调用时layouted /测量的第一次,这应该工作

If getLayoutParams().height = screenHeight; is called before the View is layouted/measured the first time, this should work.

这篇关于区别在于:myView.getLayoutParams()高度= screenHeight和myView.setLayoutParams(LP)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆