Change Screen Orientation programmatically using a Button

我认为这是可实现的,因为屏幕旋转行为可以上升到应用程序级别。

168977 次浏览

是的,这是可行的!

ActivityInfo.SCREEN _ ORIENTATION _ LANDSCAPE

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

ActivityInfo.SCREEN _ ORIENTATION _ PORTRAIT

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

活动资讯

Http://developer.android.com/reference/android/content/pm/activityinfo.html

Refer the 连结:

Button buttonSetPortrait = (Button)findViewById(R.id.setPortrait);
Button buttonSetLandscape = (Button)findViewById(R.id.setLandscape);


buttonSetPortrait.setOnClickListener(new Button.OnClickListener(){


@Override
public void onClick(View arg0) {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}


});


buttonSetLandscape.setOnClickListener(new Button.OnClickListener(){


@Override
public void onClick(View arg0) {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
}


});

Http://android-er.blogspot.in/2011/08/set-screen-orientation-programmatically.html

是的,你可以随时使用 set the screen orientation programatically:

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

方法 可用于 Activity 类,因此它可以在您的活动中使用。

这就是你如何能够 获得当前屏幕的方向和设置它充分取决于它的当前状态:

Display display = ((WindowManager) getSystemService(WINDOW_SERVICE)).getDefaultDisplay();
final int orientation = display.getOrientation();
// OR: orientation = getRequestedOrientation(); // inside an Activity


// set the screen orientation on button click
Button btn = (Button) findViewById(R.id.yourbutton);
btn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {


switch(orientation) {
case Configuration.ORIENTATION_PORTRAIT:
setRequestedOrientation (ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
break;
case Configuration.ORIENTATION_LANDSCAPE:
setRequestedOrientation (ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
break;
}
}
});

从这里拍摄: http://techblogon.com/android-screen-orientation-change-rotation-example/

剪辑

此外,你可以使用 Configuration获得屏幕方向:

Activity.getResources().getConfiguration().orientation

使用这个设置屏幕的方向:

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

或者

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

别忘了把这个加到你的旅客名单里:

android:configChanges = "orientation"

请尽可能不要使用 SCREEN _ ORIENTATION _ LANDSCAPE 或 SCREEN _ ORIENTATION _ PORTRAIT,而是使用:

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT);

这些功能允许用户将设备分别定向为横向或纵向。如果你曾经玩过一个把充电电缆插入胃里的游戏,那么你就会知道为什么这两种方向对用户来说都很重要。

注意: 对于手机,至少我已经检查了几个,它只允许“正面向上”的肖像模式,但是,SENSOR _ PORTRAIT 在平板电脑上正常工作。

注意: 这个特性是在 API 级别9中引入的,所以如果您必须支持8或更低的版本(目前不太可能) ,那么改用:

setRequestedOrientation(Build.VERSION.SDK_INT < 9 ?
ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE :
ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE);
setRequestedOrientation(Build.VERSION.SDK_INT < 9 ?
ActivityInfo.SCREEN_ORIENTATION_PORTRAIT :
ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT);

工作代码:

private void changeScreenOrientation() {
int orientation = yourActivityName.this.getResources().getConfiguration().orientation;
if (orientation == Configuration.ORIENTATION_LANDSCAPE) {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
showMediaDescription();
} else {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
hideMediaDescription();
}
if (Settings.System.getInt(getContentResolver(),
Settings.System.ACCELEROMETER_ROTATION, 0) == 1) {
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);
}
}, 4000);
}
}

call this method in your button click

Yes ، 哈里哈兰 answer works fine . but you should add below line to AndroidManifest.xml in activity tag :

android:screenOrientation="fullSensor"
android:configChanges="orientation|screenSize"

如果不加上上面的行,哈里哈拉答不上来。

Thanks 本尼 !

我的合作伙伴是:

setLandscapeIcon.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
setLandscapeIcon.setVisibility(View.GONE);
setPortraitIcon.setVisibility(View.VISIBLE);
}
});


setPortraitIcon.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
setLandscapeIcon.setVisibility(View.VISIBLE);
setPortraitIcon.setVisibility(View.GONE);
}
});

在清单上

android:configChanges = "orientation|screenSize"
android:screenOrientation="portrait"

注意: setRequestedDirection(ActivityInfo.SCREEN _ ORIENTATION _ PORTRAIT) ; 在我的设备上无法工作