LinearLayout.getWidth() 未反映来自 setLayoutParams 的新宽度
LinearLayout.getWidth() not reflecting new width from setLayoutParams
tl/dr
I am trying to change some layouts sizes in code, and I can change some of them, but the other ones that needed to update based on the
changes won't update together. Instead, they retain the previous width
that the changed ones had.
所以,我正在使用 Android Studio 并尝试以编程方式更改一些 LinearLayouts 的大小。
这就是我正在做的事情:
final LinearLayout ll3 = (LinearLayout)findViewById(R.id.box3);
ViewGroup.LayoutParams p3 = ll3.getLayoutParams();
int w3 = 5;
float aux = (float)w3/15;
p3.width = Math.round(max*aux);
ll3.setLayoutParams(p3);
然后这就是我得到的:
Log.i("OnLongClick p3_width", Integer.toString(p3.width));
Log.i("OnLongClick ll3_getWidth", Integer.toString(ll3.getWidth()));
OnLongClick p3_width﹕ 179
OnLongClick ll3_getWidth﹕ 224
ll3.getWidth() 正在返回先前的值,该值在设置新参数之前已经存在。
如果我重复这个过程,那么它会显示类似:
OnLongClick p3_width﹕ 314 //New Value
OnLongClick ll3_getWidth﹕ 179 //Old (previous) Value
我需要 ll3.getWidth() 与 p3.width 相同,因为我在另一个函数中调用它以重新计算我的应用程序具有的其他 LinearLayouts。否则app重绘时,手动改变的矩形会改变,但其他的(利用改变自己修改的)会保持原来改变的样子(对不起这句话)。
我认为发生这种情况是因为 setLayoutParams 必须及时完成它的工作,而不是在我调用 getWidth() 之前,但我不知道这是否是真正的问题,以及如何解决它.
感谢任何帮助。
提前致谢!!!
编辑2:
如果我发送
openOptionsMenu();
closeOptionsMenu();
它固定了矩形的大小。但是,它会延迟执行此操作,因此非常难看。
无论如何,我需要将任何东西放在 openOptionsMenu() 中间来解决这个问题(closeOptionsMenu() 不会改变视图,只会关闭菜单)。
步骤#1:通过getLayoutParams()获取LinearLayout的LayoutParams。这些的实际 class 将取决于 LinearLayout 的父级。
步骤 #2:修改 LayoutParams 中的值。
第 3 步:通过 setLayoutParams() 设置 LinearLayout 修改后的 LayoutParams。
找到答案了!!!就像很多次一样,它就在我的脸上。
发生的事情是 p3.width
只不过是 ll3.getLayoutParams().width
。
所以,我现在调用 ll3.getLayoutParams().width
,而不是调用 ll3.getWidth()
(return 宽度错误),然后它会正确地进行计算。
我仍然不知道为什么 ll3.getWidth()
会 return 一个错误的值,但至少它是有效的(不过,如果你知道为什么,也请告诉我!!! ).
tl/dr
I am trying to change some layouts sizes in code, and I can change some of them, but the other ones that needed to update based on the changes won't update together. Instead, they retain the previous width that the changed ones had.
所以,我正在使用 Android Studio 并尝试以编程方式更改一些 LinearLayouts 的大小。
这就是我正在做的事情:
final LinearLayout ll3 = (LinearLayout)findViewById(R.id.box3);
ViewGroup.LayoutParams p3 = ll3.getLayoutParams();
int w3 = 5;
float aux = (float)w3/15;
p3.width = Math.round(max*aux);
ll3.setLayoutParams(p3);
然后这就是我得到的:
Log.i("OnLongClick p3_width", Integer.toString(p3.width));
Log.i("OnLongClick ll3_getWidth", Integer.toString(ll3.getWidth()));
OnLongClick p3_width﹕ 179
OnLongClick ll3_getWidth﹕ 224
ll3.getWidth() 正在返回先前的值,该值在设置新参数之前已经存在。 如果我重复这个过程,那么它会显示类似:
OnLongClick p3_width﹕ 314 //New Value
OnLongClick ll3_getWidth﹕ 179 //Old (previous) Value
我需要 ll3.getWidth() 与 p3.width 相同,因为我在另一个函数中调用它以重新计算我的应用程序具有的其他 LinearLayouts。否则app重绘时,手动改变的矩形会改变,但其他的(利用改变自己修改的)会保持原来改变的样子(对不起这句话)。
我认为发生这种情况是因为 setLayoutParams 必须及时完成它的工作,而不是在我调用 getWidth() 之前,但我不知道这是否是真正的问题,以及如何解决它.
感谢任何帮助。
提前致谢!!!
编辑2: 如果我发送
openOptionsMenu();
closeOptionsMenu();
它固定了矩形的大小。但是,它会延迟执行此操作,因此非常难看。 无论如何,我需要将任何东西放在 openOptionsMenu() 中间来解决这个问题(closeOptionsMenu() 不会改变视图,只会关闭菜单)。
步骤#1:通过getLayoutParams()获取LinearLayout的LayoutParams。这些的实际 class 将取决于 LinearLayout 的父级。
步骤 #2:修改 LayoutParams 中的值。
第 3 步:通过 setLayoutParams() 设置 LinearLayout 修改后的 LayoutParams。
找到答案了!!!就像很多次一样,它就在我的脸上。
发生的事情是 p3.width
只不过是 ll3.getLayoutParams().width
。
所以,我现在调用 ll3.getLayoutParams().width
,而不是调用 ll3.getWidth()
(return 宽度错误),然后它会正确地进行计算。
我仍然不知道为什么 ll3.getWidth()
会 return 一个错误的值,但至少它是有效的(不过,如果你知道为什么,也请告诉我!!! ).