使用 configChanges 同时在横屏模式下全屏 activity

Full screen activity in landscape mode at the same time with configChanges

在我的 activity 中,我有一个 ImageView,通过单击它可以移动到 (100, 100) 坐标。我在 activity 中的 setContentView() 之前添加了以下代码,以便在 landscape 模式下获得全屏视图:

if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
    getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
}
else {
    getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
}

一切正常,当我将设备切换到横向模式时,我可以看到全屏视图。但我需要 activity 不会通过转向横向模式来重新创建。例如,如果我单击 ImageView,它移动到 (100, 100),在进入横向模式后它应该停留在这个坐标上。为此,我将这一行添加到清单中:

android:configChanges="orientation|screenSize"

但是通过将此行添加到清单,全屏 属性 停止工作。请问我怎样才能同时拥有这两个属性?

Activity :

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
    }
    else {
        getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
    }
    setContentView(R.layout.activity_main);

    final ImageView imageView = (ImageView)findViewById(R.id.imageView);
    imageView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            imageView.setX(100);
            imageView.setY(100);
        }
    });
}
}

XML :

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <ImageView
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:id="@+id/imageView"
        android:background="#000000"/>
</LinearLayout>  

清单:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.test">

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".MainActivity"
        android:configChanges="orientation|screenSize">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

</manifest>

正如 Edson Menegat 所说,我在 activity 的末尾添加了以下代码,现在我的问题已解决:

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    // Checks the orientation of the screen
    if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
    } else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){
        getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
    }
}  

我在问题中写的 setContentView() 之前的代码也是必需的。

谢谢 Edson Menegatti ...