如何获得根布局的新宽度/高度onConfigurationChanged? [英] How to get NEW width/height of root layout in onConfigurationChanged?

查看:253
本文介绍了如何获得根布局的新宽度/高度onConfigurationChanged?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们的一个看法有一个滚动型作为其根布局。当设备旋转和 onConfigurationChanged()被调用时,我们希望能够获得滚动型新宽度/高度。我们的code是这样的:

One of our views has a ScrollView as its root layout. When the device is rotated and onConfigurationChanged() is called, we'd like to be able to get the ScrollView's new width/height. Our code looks like this:

@Override
public void onConfigurationChanged(Configuration newConfig) {
    Log.d(TAG, "Width: '" + findViewById(R.id.scrollview).getWidth() + "'");
    Log.d(TAG, "Height: '" + findViewById(R.id.scrollview).getHeight() + "'");

    super.onConfigurationChanged(newConfig);

    Log.d(TAG, "Width: '" + findViewById(R.id.scrollview).getWidth() + "'");
    Log.d(TAG, "Height: '" + findViewById(R.id.scrollview).getHeight() + "'");
}

我们的Andr​​oidManifest.xml中的相关章节如下所示:

The relevant section of our AndroidManifest.xml looks like this:

<activity android:name=".SomeActivity"
    android:configChanges="keyboardHidden|orientation">
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
    </intent-filter>
</activity>

最后,我们的布局的相关部分看起来是这样的:

And finally, the relevant portion of our layout looks like this:

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/scrollview"
    android:layout_height="fill_parent"
    android:layout_width="fill_parent"
    >
    <LinearLayout android:id="@+id/container"
        android:orientation="vertical"
        android:layout_height="fill_parent"
        android:minHeight="200dip"
        android:layout_width="fill_parent"
        >

在我们的Droid,我们期望看到的滚动型的宽度到854时,开机时回纵向切换到横向,和480(和高度做等价交换,减去菜单栏)。然而,我们看到的相反。下面是我们的LogCat中:

On our Droid, we expected to see the ScrollView's width go to 854 when switched into landscape, and to 480 when switched back to portrait (and the height do the equivalent switch, minus the menu bar). However, we're seeing the opposite. Here's our LogCat:

// Switching to landscape:
03-26 11:26:16.490: DEBUG/ourtag(17245): Width: '480'  // Before super
03-26 11:26:16.490: DEBUG/ourtag(17245): Height: '778' // Before super
03-26 11:26:16.529: DEBUG/ourtag(17245): Width: '480'  // After super
03-26 11:26:16.536: DEBUG/ourtag(17245): Height: '778' // After super

// Switching to portrait:
03-26 11:26:28.724: DEBUG/ourtag(17245): Width: '854'  // Before super
03-26 11:26:28.740: DEBUG/ourtag(17245): Height: '404' // Before super
03-26 11:26:28.740: DEBUG/ourtag(17245): Width: '854'  // After super
03-26 11:26:28.740: DEBUG/ourtag(17245): Height: '404' // After super

显然,我们得到的画像尺寸,当我们切换到横向,景观方面,当我们切换到肖像。有没有什么我们做错了什么?我们可以得到哈克和解决这个问题,但我觉得有,我们错过了一个简单的解决方案。

Clearly, we're getting the portrait dimensions when we switch to landscape, and the landscape dimensions when we switch to portrait. Is there something we're doing wrong? We could get hacky and solve this, but I feel like there's a simple solution that we're missing.

推荐答案

getWidth()返回的宽度作为视图的布局,这意味着你需要等待,直到它被绘制到屏幕上。 onConfigurationChanged 之前调用重绘视图的新配置,所以我不认为你就可以得到你的新宽度,直到后来。

getWidth() returns the width as the View is laid out, meaning you need to wait until it is drawn to the screen. onConfigurationChanged is called before redrawing the view for the new configuration and so I don't think you'll be able to get your new width until later.

这篇关于如何获得根布局的新宽度/高度onConfigurationChanged?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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