我无法正确调整元素(例如 ListView
)的大小。
我有一个用于搜索的 ListActivity
,带有仪表板、编辑框和 ListView 。这个想法是在键盘显示时隐藏操作栏。
我扩展了 LinearLayout
,以监听 View 大小的变化(推荐 here):
public class SizeChangingLinearLayout extends LinearLayout {
//...
@Override
protected void onSizeChanged(int xNew, int yNew, int xOld, int yOld)
{
View actionbar = mainView.findViewById(R.id.actionbar);
if (yNew > yOld)
actionbar.setVisibility(View.VISIBLE);
else if (yNew < yOld)
actionbar.setVisibility(View.GONE);
super.onSizeChanged(xNew, yNew, xOld, yOld);
}
}
actionbar 的隐藏/显示按预期工作,但 ListView
下方与隐藏的 actionbar 高度相同。一旦任何图形发生变化(例如在编辑框中写入),ListView
就会填补空白。隐藏键盘时,类似的行为会反过来出现。
[编辑]
更改其他布局时也会出现同样的问题。更改 onSizeChanged
中事物的 Visibility
会直接显示,但在其他用户操作重绘这些 View 之前,更改大小和边距不会显示。 onSizeChanged
无效。
[/编辑]
我尝试通过以下方式刷新图形:
invalidate
-ing 自定义 LinearLayout 和ListView
(如 推荐here )notifyDataSetChanged
在列表的 适配器forceLayout
...还有更多,但没有成功。为什么 ListView
一开始没有调整大小?我是否必须在扩展的 LinearLayout 中覆盖 onLayout
?我错过了什么?
最佳答案
在onSizeChanged()
的末尾添加以下内容即可解决:
Handler handler = new Handler();
handler.post(new Runnable() {
@Override
public void run() {
requestLayout();
}
});
查看此问题的答案:Views inside a custom ViewGroup not rendering after a size change
关于android - 如何在 onSizeChanged 中设置大小和布局?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6262265/