Need Help in Changing Screen Orientation in Android

﹥>﹥吖頭↗ 提交于 2019-12-20 04:55:39

问题


I want to change the screen orientation from portrait to landscape and vice versa when the user rotates his Android mobile phone clockwise/anticlockwise. Can anyone help me how to achieve this? I do not know what event is fired when the user rotates his Android mobile phone.

For instance, when the user touches the button in UI, touch event is fired. So, when the user shakes his Android mobile phone, what is the event that is fired? Please help me on this.


回答1:


Hai, I got Solution with the help of Georgy Gobozov, But that solution has to be refined as shown below to work it properly,

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

     if(newConfig.equals(Configuration.ORIENTATION_LANDSCAPE))
     {
         setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);  
     }
     else if(newConfig.equals(Configuration.ORIENTATION_PORTRAIT))
     {
          setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
     }
    } 



回答2:


Just add to your AndroidManifest in activity

android:configChanges="keyboardHidden|orientation"

Then you should override this method in your activity

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

And use search, this question is very popular.




回答3:


 public class Accelerometer extends Activity 
    implements AccelerometerListener{

    public void onShake(float force) {
         Toast.makeText(this, "Phone shaked : " + force, 1000).show();
    }
 }

You can refer to below link

http://blog.androgames.net/85/android-accelerometer-tutorial/



来源:https://stackoverflow.com/questions/4071717/need-help-in-changing-screen-orientation-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!