Java 需要安卓定位技巧

Java 需要安卓定位技巧,java,android,orientation,Java,Android,Orientation,所以我在活动中有一个web视图 我需要它保持方向与用户打开活动时设备所在的方向相同,所以如果在活动打开时横向->保持该方向!。。如果你想留着它 所以 不需要XML清单表示法。。我不想强迫一个特定的方向,只是为了保持一个开放的方向! 这两种可能性都不起作用: public void onConfigurationChanged(Configuration newConfig) { super.onConfigurationChanged(null); 或 那么,关于固定活动导向的想法呢

所以我在活动中有一个web视图

我需要它保持方向与用户打开活动时设备所在的方向相同,所以如果在活动打开时横向->保持该方向!。。如果你想留着它

所以

不需要XML清单表示法。。我不想强迫一个特定的方向,只是为了保持一个开放的方向! 这两种可能性都不起作用:

 public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(null);


那么,关于固定活动导向的想法呢

设置马尼宴会活动

android:screenOrientation="portrait"


您应该仔细阅读Androids活动生命周期。 重新打开活动时,将生成一个新实例。因此,没有以前的方向。 如果您想要与暂停活动时相同的方向,则应将以前的方向存储在一个在整个生命周期中持续存在的值中


编辑:如果你想让你的示例生效,你应该在清单中声明它们。

将android:configChanges=orientation放在你的活动下的清单中,告诉android该活动将自行处理屏幕方向的更改。

我不确定你为什么要实现该功能,但这应该有效:

  @Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if(savedInstanceState == null) // you only want to save orientation when you are loading the activity for the first time
    {
        ((MyApp)getApplication()).orientation = getResources().getConfiguration().orientation; // I saved the orientation in the application class, but you can save it anywhere you like except in "this"
        Log.d("test", "orientation in onCreate: "+((MyApp)getApplication()).orientation);
    }

}
@Override
protected void onResume() {
    super.onResume();
    Log.d("test", "setContentview with orientation in onResume "+((MyApp)getApplication()).orientation);
    if(((MyApp)getApplication()).orientation==Configuration.ORIENTATION_LANDSCAPE)
    {
        setContentView(R.layout.my_layout_portrait);
    }
    else if(((MyApp)getApplication()).orientation ==Configuration.ORIENTATION_PORTRAIT)
    {
        setContentView(R.layout.my_layout_landscape);
    }
}

另一方面,对我来说,强制这样的方向是个坏主意。

再次强调,我不想强制特定的方向。只保留活动实例化时使用的方向!如果我不这样做,webview js+html会有奇怪的外观!哦,我又有了一个好主意。为什么不在活动开始前检查一下方向?类似于iforientation==Configuration.ORIENTATION\u的内容
android:screenOrientation="landscape"
  @Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if(savedInstanceState == null) // you only want to save orientation when you are loading the activity for the first time
    {
        ((MyApp)getApplication()).orientation = getResources().getConfiguration().orientation; // I saved the orientation in the application class, but you can save it anywhere you like except in "this"
        Log.d("test", "orientation in onCreate: "+((MyApp)getApplication()).orientation);
    }

}
@Override
protected void onResume() {
    super.onResume();
    Log.d("test", "setContentview with orientation in onResume "+((MyApp)getApplication()).orientation);
    if(((MyApp)getApplication()).orientation==Configuration.ORIENTATION_LANDSCAPE)
    {
        setContentView(R.layout.my_layout_portrait);
    }
    else if(((MyApp)getApplication()).orientation ==Configuration.ORIENTATION_PORTRAIT)
    {
        setContentView(R.layout.my_layout_landscape);
    }
}