在我的活动中,我有一个ImageView,通过点击它移动到(100,100)坐标。我在活动中的setContentView()之前添加了以下代码,以便在横向模式下获得全屏视图:
if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
}
else {
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
}
一切正常,当我将设备转为横向模式时,我有一个全屏视图。但我需要通过转向横向模式来重建活动。例如,如果我单击ImageView并将其移至(100,100),则在转到横向模式后,它应该保持在此坐标上。为此,我将此行添加到Manifest:
android:configChanges="orientation|screenSize"
但是通过将此行添加到清单,全屏属性将停止工作。我怎样才能同时拥有这两个属性?
活动:
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>
答案 0 :(得分:1)
正如Edson Menegatt所说,我在活动结束时添加了以下代码,现在问题解决了:
@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 ......