Android:addView 和 layout 方法不能一起工作 [英] Android: addView and layout method does not work together

查看:38
本文介绍了Android:addView 和 layout 方法不能一起工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Android 中,当我将 ImageView 等视图对象添加到 FrameLayout 视图,然后立即尝试调用 layout() 方法来重新定位添加的视图对象 时,它不是在职的.但是当我在一些新事件之后单独调用布局方法时,例如触摸事件然后 layout() 似乎工作. 当一起使用 combineToFront() 和 layout() 时也会发生这种情况, layout() 似乎无法与其他方法一起使用.这有什么原因吗?也可以在 FrameLayout 的某个特定位置添加视图对象,而不是在屏幕中间添加视图对象?

In Android when I am adding a view object like ImageView to FrameLayout view and then immediately trying to call layout() method to repositioned the added view object it is not working. But when I call the layout method alone after some new event e.g. touch event then layout() seems to work. This also happens when bringToFront() and layout() is used together, layout() does not seems to work together with other method. Is there any reason why is this like this? Also instead of adding view object in the middle of the screen can it be ADDED at some specific location in FrameLayout?

推荐答案

你不应该直接调用 layout().这仅由框架使用.您必须修改 View 的布局参数 (getLayoutParams()) 并调用 requestLayout().

You should NEVER call layout() directly. This is used by the framework only. You must modify the View's layout params (getLayoutParams()) and call requestLayout().

这篇关于Android:addView 和 layout 方法不能一起工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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