如何处理方向改变很容易 [英] How to handle orientation change easily

查看:179
本文介绍了如何处理方向改变很容易的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我工作的一个Android应用,programmaticaly增加了每个视图。
当用户开启屏幕,我只想被填充再次显示的值。结果
有一个简单的方法,让Android的做到这一点automaticaly?

I'm working on a android application that adds every view programmaticaly. When the user turns the screen, I just want to values that are filled in to be displayed again.
Is there an easy way to let Android do this automaticaly?

我的应用程序是完全动态的,所以它好好尝试一下有predetermined布局,这使得它变得更加困难。

My application is completely dynamic so it doens't have a predetermined layout, which makes it a lot harder.

所以,我怎么能轻易救我的屏幕的状态?

So, how can I easily save the state of my screen?

推荐答案

每次改变方向时,机器人创造新的观点和摧毁旧的。您可以保存数据时的方向变化和新视图创建时重新初始化

Everytime orientation change, android create new view and destroy the old one. You can saved your data when orientation change and re-initialize when the new view is created

使用 onConfigurationChanged 活动的方法来检测取向变化

Use onConfigurationChanged method of activity to detect Orientation Change

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

不要忘了在AndroidManifest.xml中编辑适当的元素像这样包括的android:configChanges

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

这篇关于如何处理方向改变很容易的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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