如何设置大小和布局onSizeChanged? [英] How to set size and layout in onSizeChanged?
问题描述
我不能让我的元素,例如的ListView
,调整大小正常。
I can't get my elements, for example ListView
, to resize properly.
我有一个 ListActivity
搜索,与仪表板,一个编辑框和列表视图。我们的想法是隐藏动作条,而键盘呈现。
I got a ListActivity
for searching, with a dashboard, an editbox and a listview. The idea is to hide the actionbar while the keyboard is showing.
我延长的LinearLayout
,听取查看大小的变化(如建议的<一个href="http://stackoverflow.com/questions/3825918/way-to-be-notified-when-the-software-keyboard-is-showing-hiding">here)
I extended LinearLayout
, to listen to view size changes (as recommended 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);
}
}
在动作条的隐藏/显示工作正常,但有以下的ListView相同的高度
的间隙为隐藏的动作条。只要任何图形变化(例如书写的编辑框)的的ListView
填补了这个空白。类似的行为出现在倒车时,隐藏键盘的时候。
The hiding/showing of the actionbar works as expected but there is a gap below the ListView
of the same height as the hidden actionbar. As soon as any graphics change (e.g. writing in the editbox) the ListView
fills the gap. A similar behavior appears in reverse, when hiding the keyboard.
同样的问题也出现在其他的变化布局。更改能见度
的东西 onSizeChanged
直接显示,但是改变的大小和利润率不显示,直到其他用户操作重新绘制这些观点。失效不会从 onSizeChanged
工作
The same problem also appears in changing other layout. Changing Visibility
of things in onSizeChanged
shows directly, but changing sizes and margins does not show until some other user action redraws those views. Invalidation does not work from onSizeChanged
.
[/编辑]
我试图通过刷新显卡:
-
废止
-ing两个自定义 的LinearLayout和的ListView
(如 推荐使用<一个href="http://stackoverflow.com/questions/1017853/android-how-to-redraw-a-graphic-element">here) -
notifyDataSetChanged
的列表中的 适配器 -
forceLayout
invalidate
-ing both the custom LinearLayout and theListView
(as recommended here)notifyDataSetChanged
on the list's adapterforceLayout
...等等,都没有成功。为什么的ListView
未调整的第一?我一定要重写 onLayout
在我的扩展的LinearLayout?什么我错过了?
... and more, without success. Why is the ListView
not resizing at first? Do I have to override onLayout
in my extended LinearLayout? What have I missed?
推荐答案
添加以下内容在年底 onSizeChanged()
将解决这个问题:
Adding the following at the end of onSizeChanged()
will solve it:
Handler handler = new Handler();
handler.post(new Runnable() {
@Override
public void run() {
requestLayout();
}
});
查看回答这个问题:<一href="http://stackoverflow.com/questions/5852758/views-inside-a-custom-viewgroup-not-rendering-after-a-size-change">Views自定义内的大小变化
这篇关于如何设置大小和布局onSizeChanged?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!