安卓:监听取向的变化?取向

2023-09-04 06:23:26 作者:姐看不上连葱都不是的你

我如何会听的方向变化,Android的?和做某些事情的时候用户切换到风景?

How would I listen for orientation change in Android? and do certain things when the user has switched to landscape?

推荐答案

您有几个选择:

使用的OrientationEventListener,其中有一个称为方法onOrientationChanged.

Use an OrientationEventListener, which has a method called onOrientationChanged.

使用配置的变化:

在你的清单,把:

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

和,在code,覆盖onConfigurationChanged

And, in code, override onConfigurationChanged

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    setContentView(R.layout.main);      
    // TODO
}

下面是一个很好的tutorial一下吧。

Here is a good tutorial about it.