如何检测的方向变化,布局Android的? [英] How to detect orientation change in layout in Android?

查看:119
本文介绍了如何检测的方向变化,布局Android的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚实施的方向变化的功能。

I just implemented the orientation change feature.

当从纵向到横向(反之亦然),我怎么可以检测方向更改事件完成了布局的变化。

When the layout changes from portrait to landscape (or vice versa), how can I detect when the orientation change event finished.

我试图用 OrientationEventListener ,但它似乎不是一个好主意。

I tried to use OrientationEventListener, but it seems not a good idea.

我只想得到通知,只有当布局方向变化。那怎么办?

I just want to be notified only when layout orientation changes. How to do that?

推荐答案

使用 onConfigurationChanged 活动的方法。 请参见下面的code:

Use the onConfigurationChanged method of Activity. See the following code:

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);

    // Checks the orientation of the screen
    if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
        Toast.makeText(this, "landscape", Toast.LENGTH_SHORT).show();
    } else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){
        Toast.makeText(this, "portrait", Toast.LENGTH_SHORT).show();
    }
}

您还必须修改相应的 元素在你的manifest文件包含机器人:configChanges 请看下面的code:

You also have to edit the appropriate element in your manifest file to include the android:configChanges Just see the code below:

<activity android:name=".MyActivity"
          android:configChanges="orientation|keyboardHidden"
          android:label="@string/app_name">

注意:与Android 3.2(API级别13)或更高时,屏幕尺寸也改变时,纵向和横向之间的设备切换。因此,如果你开发的API级别13或更高时,要因方向变化prevent运行时重新启动,则必须decalare 机器人:configChanges =方向|屏幕尺寸的API级别13或更高

NOTE: with Android 3.2 (API level 13) or higher , the "screen size" also changes when the device switches between portrait and landscape orientation. Thus, if you want to prevent runtime restarts due to orientation change when developing for API level 13 or higher , you must decalare android:configChanges="orientation|screenSize" for API level 13 or higher .

希望这会帮助你..:)

Hope this will help you..:)

这篇关于如何检测的方向变化,布局Android的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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