Android 定向机器人
我想在我的应用程序上添加方向,但我需要-->当我的手机在纵向样式上运行时,以及当我在横向样式A活动停止而B活动开始时更改纵向样式时。我如何处理此问题?谢谢…首先检查方向:Android 定向机器人,android,screen-orientation,android-orientation,Android,Screen Orientation,Android Orientation,我想在我的应用程序上添加方向,但我需要-->当我的手机在纵向样式上运行时,以及当我在横向样式A活动停止而B活动开始时更改纵向样式时。我如何处理此问题?谢谢…首先检查方向: if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) { } if (getResources().getConfiguration().orientation ==Configuration.O
if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT)
{
}
if (getResources().getConfiguration().orientation ==Configuration.ORIENTATION_LANDSCAPE)
{
}
Display display = ((WindowManager) getSystemService(WINDOW_SERVICE)).getDefaultDisplay();
int orientation = display.getOrientation();
并根据方向的值启动所需的活动
然后在“活动”中覆盖此方法:
public void onConfigurationChanged(Configuration newConfig) {
//start the other activity
}
首先,按以下方式检查方向:
Display display = ((WindowManager) getSystemService(WINDOW_SERVICE)).getDefaultDisplay();
int orientation = display.getOrientation();
并根据方向的值启动所需的活动
然后在“活动”中覆盖此方法:
public void onConfigurationChanged(Configuration newConfig) {
//start the other activity
}
这样做
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
// Checks the orientation of the screen
if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
Toast.makeText(this, "landscape", Toast.LENGTH_SHORT).show();
//here call activity A
} else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){
Toast.makeText(this, "portrait", Toast.LENGTH_SHORT).show();
//here call activity B
}
}
这样做
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
// Checks the orientation of the screen
if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
Toast.makeText(this, "landscape", Toast.LENGTH_SHORT).show();
//here call activity A
} else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){
Toast.makeText(this, "portrait", Toast.LENGTH_SHORT).show();
//here call activity B
}
}
使用以下代码开始您的
活动B
:
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
// Checks the orientation of the screen
if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
Intent a = new Intent(this, B.class);
startActivity(a);
} else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){
//do nothing
}
}
并且,在您的AndroidManifest.xml
文件的两个活动标记中。请按下面的方式申报
<activity android:name=".A"
android:configChanges="orientation|keyboardHidden"
android:label="@string/app_name">
用以下代码开始您的
活动B
:
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
// Checks the orientation of the screen
if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
Intent a = new Intent(this, B.class);
startActivity(a);
} else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){
//do nothing
}
}
并且,在您的AndroidManifest.xml
文件的两个活动标记中。请按下面的方式申报
<activity android:name=".A"
android:configChanges="orientation|keyboardHidden"
android:label="@string/app_name">