如何在 onConfigurationChanged 中获取根布局的新宽度/高度?

发布于 2024-08-26 21:28:44 字数 2238 浏览 4 评论 0原文

我们的视图之一以 ScrollView 作为其根布局。当设备旋转并调用 onConfigurationChanged() 时,我们希望能够获取 ScrollView 的新宽度/高度。我们的代码如下所示:

@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() + "'");
}

AndroidManifest.xml 的相关部分如下所示:

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

最后,布局的相关部分如下所示:

<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 上,我们期望看到 ScrollView 的宽度在切换到横向时变为 854,当切换回纵向时为 480(并且高度进行等效切换,减去菜单栏)。然而,我们看到的却恰恰相反。这是我们的 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

显然,当我们切换到横向时,我们会得到纵向尺寸,而当我们切换到纵向时,我们会得到横向尺寸。我们是不是做错了什么?我们可以通过一些技巧来解决这个问题,但我觉得我们缺少一个简单的解决方案。

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() + "'");
}

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"
        >

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

瞄了个咪的 2024-09-02 21:28:45

当 onGlobalLayout 调用时,不确定视图是否已根据新的布局方向调整大小,因此对我来说,只有以下解决方案可以正常工作:

@Override
public void onConfigurationChanged(Configuration newConfig) {
final int oldHeight = mView.getHeight();
final int oldWidth = mView.getWidth();

mView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {
            if (mView.getHeight() != oldHeight && mView.getWidth() != oldWidth) {
                mView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
                //mView now has the correct dimensions, continue with your stuff
            }
        }
    });
    super.onConfigurationChanged(newConfig);}

When onGlobalLayout called it's not sure that the view has been resized accordingly to the new layout orientation, so for me only the below solution worked properly:

@Override
public void onConfigurationChanged(Configuration newConfig) {
final int oldHeight = mView.getHeight();
final int oldWidth = mView.getWidth();

mView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {
            if (mView.getHeight() != oldHeight && mView.getWidth() != oldWidth) {
                mView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
                //mView now has the correct dimensions, continue with your stuff
            }
        }
    });
    super.onConfigurationChanged(newConfig);}
巨坚强 2024-09-02 21:28:45

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.

合久必婚 2024-09-02 21:28:44

对于那些寻求更详细的解决方案描述的人:您可以使用视图的 ViewTreeObserver 并注册一个 OnGlobalLayoutListener

@Override
public void onConfigurationChanged(Configuration newConfiguration) {
    super.onConfigurationChanged(newConfiguration);
    final View view = findViewById(R.id.scrollview);

    ViewTreeObserver observer = view.getViewTreeObserver();
    observer.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {

        @Override
        public void onGlobalLayout() {
            Log.v(TAG,
                    String.format("new width=%d; new height=%d", view.getWidth(),
                            view.getHeight()));
            view.getViewTreeObserver().removeOnGlobalLayoutListener(this);
        }
    });
}

For those looking for a more detailed solution description: You can use the ViewTreeObserver of your view and register an OnGlobalLayoutListener.

@Override
public void onConfigurationChanged(Configuration newConfiguration) {
    super.onConfigurationChanged(newConfiguration);
    final View view = findViewById(R.id.scrollview);

    ViewTreeObserver observer = view.getViewTreeObserver();
    observer.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {

        @Override
        public void onGlobalLayout() {
            Log.v(TAG,
                    String.format("new width=%d; new height=%d", view.getWidth(),
                            view.getHeight()));
            view.getViewTreeObserver().removeOnGlobalLayoutListener(this);
        }
    });
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文