我正试图实现这种行为:
问题: 将屏幕重新设置为纵向的按钮工作正常,但我希望能够将设备旋转到横向并旋转屏幕,但它仍保持锁定状态。
此行为类似于youtube播放器,您可以旋转或单击按钮退出全屏。
我的按钮代码:
findViewById(R.id.exit_fs).setOnClickListener(new Button.OnClickListener(){
@Override
public void onClick(View arg0) {
setRequestedOrientation(Build.VERSION.SDK_INT < 9 ?
ActivityInfo.SCREEN_ORIENTATION_PORTRAIT :
ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT);
}
});
有什么想法吗?
答案 0 :(得分:1)
我发现这种方法非常好:
// E.g. fullscreen button pressed - set landscape orientation;
// it will disable orientation by the sensor.
setRequestedOrientation(SCREEN_ORIENTATION_LANDSCAPE)
// Assume now user rotated the device... or not.
// Anyway enable orientation by the sensor after few seconds, so user may use app normal way.
someView.postDelayed(
{ setRequestedOrientation(SCREEN_ORIENTATION_SENSOR) }, delay = 5000L)
答案 1 :(得分:1)
首先从Android设置中启用“自动旋转”。
然后在要配置“视频到横向”模式的“活动”中使用以下代码。
重写fun onConfigurationChanged(newConfig:Configuration){ super.onConfigurationChanged(newConfig)
if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) {
// if you tilt your screen to Portrait mode
// send your seek position with intent to continue watching video to your previous screen
finish()
}else{
// continue to playing video
}
}
在清单文件中,将screenOrientation设置为“ fullSensor”,如下所示:
android:configChanges="keyboard|keyboardHidden|orientation|screenSize|screenLayout|smallestScreenSize|uiMode"
android:screenOrientation="fullSensor"
答案 2 :(得分:0)
我遇到了同样的问题。我最后使用的是OrientationListener来检测用户何时实际将手机倾斜到横向,然后将方向设置为SCREEN_ORIENTATION_SENSOR。
`OrientationEventListener orientationEventListener = new OrientationEventListener(getActivity()) {
@Override
public void onOrientationChanged(int orientation) {
int epsilon = 10;
int leftLandscape = 90;
int rightLandscape = 270;
if(epsilonCheck(orientation, leftLandscape, epsilon) ||
epsilonCheck(orientation, rightLandscape, epsilon)){
getMainActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);
}
}
private boolean epsilonCheck(int a, int b, int epsilon) {
return a > b - epsilon && a < b + epsilon;
}
};
orientationEventListener.enable();`
答案 3 :(得分:0)
在您的清单文件中,将 screenOrientation 设置为“fullSensor”,如下所示:
android:configChanges="keyboard|keyboardHidden|orientation|screenSize|screenLayout|smallestScreenSize|uiMode" android:screenOrientation="fullSensor"
答案 4 :(得分:-1)
使用此:
android.provider.Settings.System.putInt(getContentResolver(), android.provider.Settings.System.USER ROTATION,user_rotation);
user_rotation 可以是0,1,2或3;
您需要在清单中添加下一行。
<uses-permission android:name="android.permission.WRITE_SETTINGS"></uses-permission>
PD:如果你曾试图搜索,你可能已经找到了一些东西。就像在这个Post by Riddhish.Chaudhari 中一样
答案 5 :(得分:-1)
然后尝试在更改屏幕旋转之后/之前使用方法启用/禁用自动旋转。代码:
import android.provider.Settings;
public static void setAutoOrientationEnabled(Context context, boolean enabled)
{
Settings.System.putInt( context.getContentResolver(), Settings.System.ACCELEROMETER_ROTATION, enabled ? 1 : 0);
}
尝试在使用按钮旋转之前启用自动旋转