Android 更改布局问题
我是一名android应用程序开发人员。我想在我的应用程序中执行以下操作 当我点击一个布局按钮时,它会变成横向。如果我再点击这个按钮,它会变成纵向。我知道这很简单Android 更改布局问题,android,android-layout,android-studio,screen-orientation,Android,Android Layout,Android Studio,Screen Orientation,我是一名android应用程序开发人员。我想在我的应用程序中执行以下操作 当我点击一个布局按钮时,它会变成横向。如果我再点击这个按钮,它会变成纵向。我知道这很简单 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); 但有一个陷阱 当点击按钮后,它处于横向模式,我旋转设备,它也检测到设备旋转,并应更改为纵向视图 我们知道,在设置setRequestedOrientation OnConfiguration之后,从
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
但有一个陷阱
当点击按钮后,它处于横向模式,我旋转设备,它也检测到设备旋转,并应更改为纵向视图
我们知道,在设置setRequestedOrientation OnConfiguration之后,从未调用过Changed。那我该怎么做呢
我添加了以下代码:
toggleButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (toggleButton.getText().toString().trim().equalsIgnoreCase("landscape")){
landscapeMode(true);
toggleButton.setText("portrait");
} else {
landscapeMode(false);
toggleButton.setText("landscape");
}
}
});
public void toggleScreen(boolean fullscreen) {
if (landscapeMode) {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
//After this it is not detecting device rotation but it is called when I am clicking on the button first time.
} else {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);
//After this it is detecting device rotation but it is called when I am clicking on the button second time.
}
}
这是因为默认情况下,您必须在清单文件中将活动设置为“Potrait”。像这样
<activity android:name=".MainActivity"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
此外,您还可以在屏幕方向更改时保存数据。使用
@Override
public void onSaveInstanceState(final Bundle outState, PersistableBundle outPersistentState) {
super.onSaveInstanceState(outState, outPersistentState);
// Save UI state changes to the savedInstanceState.
// This bundle will be passed to onCreate if the process is
// killed and restarted.
outState.putBoolean("landscape", true);
// etc.
}
并将保存的数据保存到-
@Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
// Restore UI state from the savedInstanceState.
// This bundle has also been passed to onCreate.
boolean myBoolean = savedInstanceState.getBoolean("landscape");
}
这是因为默认情况下,您必须在清单文件中将活动设置为“Potrait”。像这样
<activity android:name=".MainActivity"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
此外,您还可以在屏幕方向更改时保存数据。使用
@Override
public void onSaveInstanceState(final Bundle outState, PersistableBundle outPersistentState) {
super.onSaveInstanceState(outState, outPersistentState);
// Save UI state changes to the savedInstanceState.
// This bundle will be passed to onCreate if the process is
// killed and restarted.
outState.putBoolean("landscape", true);
// etc.
}
并将保存的数据保存到-
@Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
// Restore UI state from the savedInstanceState.
// This bundle has also been passed to onCreate.
boolean myBoolean = savedInstanceState.getBoolean("landscape");
}
首先,非常感谢你的回复。但是它不起作用。首先,非常感谢你的回复。但是它不起作用