1

So i have a web view in a activity

I need it to keep orientation the same as the one the devices was in, when user opened activity (so if LANDSCAPE when activity was opened -> keep that!..if PORTRAIT -> keep that)

So

  • dont need XML manifest notation.. i dont want to force a specific orientation, just to keep opened one!

These two tentatives do nothing:

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

or

public void onConfigurationChanged(Configuration newConfig) {
      newConfig.orientation = getResources().getConfiguration().orientation;
      super.onConfigurationChanged(newConfig);

So ideas??

4

4 回答 4

0

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

于 2012-05-22T11:48:49.907 回答
0

用于修复活动方向

设置Mani-feast活动

android:screenOrientation="portrait"

或者

android:screenOrientation="landscape"
于 2012-05-22T11:44:51.920 回答
0

您应该阅读 Android 活动生命周期。当您重新打开一个活动时,会构建一个新实例。因此,没有“以前的”方向。如果您想要与暂停活动时相同的方向,则应将先前的方向存储在一个贯穿整个生命周期的值中。

编辑:如果你想让你的例子工作,你应该在清单中声明它们。

于 2012-05-22T11:45:21.687 回答
0

我不确定您为什么要实现该功能,但这应该可以:

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

顺便说一句,对我来说,强制这样的方向是个坏主意。

于 2012-05-22T12:14:51.340 回答