android中如何监听窗口旋转完成?

问题描述 投票:0回答:3
android android-orientation
3个回答
1
投票

我想,你想要的是当在布局上绘制视图时应该调用你的

doSomeThing()
方法。

所以你需要使用

View.post()
方法来实现这一点。 看看下面的例子:

fab.post(new Runnable() {
        @Override
        public void run() {
            // do something here 
            //......
            //......
            Toast.makeText(MainActivity.this, "Orientation has changed.", Toast.LENGTH_SHORT).show();
        }
    });

这里的 fab 是一个视图,当它被绘制到屏幕上时,你想要对其执行操作(所以你不需要使用

TimerTask
)。

正如您所说,当方向改变时您想要更新

SurfaceView
。您可以使用表面视图的
post
方法(如上面代码中的
fab
)。


-1
投票

您的

onCreate()
方法将在您的方向更改后被触发。 因此,在您的情况下,您可以在
doSomeThing()
方法中调用
onCreate()

@Override
protected void onCreate(Bundle savedInstanceState) {
      ...
      doSomething();
}

希望这有帮助


-2
投票
@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();
    }
}

如果您想了解更多信息,可以查看文档这里

© www.soinside.com 2019 - 2024. All rights reserved.