奇怪kindle火布局问题
我写了一个视图的android应用程序中的一个视图的包装的getHeight()函数使用一个OnGlobalLayoutListener附加到视图的包装的viewTreeObserver确定其空间量的回调必须在onCreate期间使用。通常这比xml下面的chordDisplay要求的400px要小。这适用于android模拟器,适用于使用android 2.1和4.03的各种屏幕尺寸。然而,在我的点燃中,当我在横向上启动应用程序(小于400px可用)时,回调最初并不会返回正确的高度,除非我手动或通过代码将方向切换为纵向并回到横向。这意味着我的视图的内容最初的大小不正确。奇怪kindle火布局问题
样品logcat的输出,当我最初推出应用在景观:
04-22 17:31:28.249: D/onGlobalLayout(12979): chordDisplay.getHeight(): 400
然后切换到肖像和回景观:
04-22 17:32:44.546: D/onGlobalLayout(12979): chordDisplay.getHeight(): 350
我不明白这怎么可能会发生考虑到在定位更改期间发生的所有事情都是在应用程序中对onCreate()的另一个调用,对吧?当我启动应用程序时会发生同样的情况。
有没有人有类似的方向切换/布局错误在他们的应用程序的任何经验?/有任何想法,为什么会发生这种情况?任何帮助将不胜感激......谢谢。
代码:
OnGlobalLayoutListener thelistener;
RelativeLayout chordDisplayWrapper;
TableRow.LayoutParams lp;
private ChordAdapter chordAdapter;
private HorizontalListView chordDisplay
thelistener = new OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
chordDisplayWrapper.getViewTreeObserver().removeGlobalOnLayoutListener(this);
Log.d("onGlobalLayout", "chordDisplay.getHeight(): " + chordDisplay.getHeight());
lp = new TableRow.LayoutParams(chordDisplay.getHeight()/6 -2,chordDisplay.getHeight()/6 -2); //6 just works. I shrink it a tiny bit to compensate for some weird bug.
chordAdapter = (new ChordAdapter(mcontext, R.layout.row, magicbundle, lp));
chordDisplay.setAdapter(chordAdapter);
}
};
chordDisplayWrapper.getViewTreeObserver().addOnGlobalLayoutListener(thelistener);
的看法XML布局介入:
<RelativeLayout
android:id="@+id/chordDisplayWrapper"
android:layout_width="wrap_content"
android:layout_height="0dip"
android:layout_weight="1"
android:gravity="center" >
<berry.chordsfree.HorizontalListView
android:id="@+id/testerList"
android:layout_width="wrap_content"
android:layout_height="400px"
android:layout_centerInParent="true"
android:gravity="center" />
</RelativeLayout>
你也可以通过现在解决了这一点,但发布为后人对于那些后可能遇到此 -
我在Nexus One上遇到了同样的问题,运行自定义的2.3.7 ROM,但代码在我的Nexus Galaxy 4.1.1版上运行。
与您类似,我试图在onCreate
期间使用ViewTreeObserver
调整某些视图的大小。我试过手动拨打dispatchGlobalLayout
,并确定这不是onGlobalLayout
未触发的问题。然后,我将移动元素移动到活动的onWindowFocusChanged
中,但没有成功(但我想避免在此处进行大量调整)。这花了我多于一天的工作,所以我现在只是使用快速/脏的修补程序。如果我只是在onCreate
完成后调整布局/视图,通常会显示:
@Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
if (!reset) {
LinearLayout frameItem = (LinearLayout) findViewById(R.id.frameItem);
frameItem.setVisibility(LinearLayout.GONE);
frameItem.setVisibility(LinearLayout.VISIBLE);
reset = true;
}
}