我的应用程序中有一个切换按钮。我想通过编程更改或控制默认设置,即自动旋转屏幕(设置>显示>自动旋转屏幕)。有人知道如何做到这一点吗?
我的应用程序中有一个切换按钮。我想通过编程更改或控制默认设置,即自动旋转屏幕(设置>显示>自动旋转屏幕)。有人知道如何做到这一点吗?
你在 Activity 中尝试过这个吗?
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
//This is the default value
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
之后,您可以使用以下内容来禁用自动方向调整:
public static void setAutoOrientationEnabled(ContentResolver resolver, boolean enabled)
{
Settings.System.putInt(resolver, Settings.System.ACCELEROMETER_ROTATION, enabled ? 1 : 0);
}
android.provider.Settings.System.putInt(getContentResolver(),
android.provider.Settings.System.USER ROTATION,user_rotation);
user_rotation 0 -> ROTATION_0
user_rotation 1 -> ROTATION_90
user_rotation 2 -> ROTATION_180
user_rotation 3 -> ROTATION_270
请查看http://developer.android.com/reference/android/provider/Settings.System.html#USER_ROTATION以获取更多信息。
此外,还有menifest.xml设置。
<uses-permission android:name="android.permission.WRITE_SETTINGS"></uses-permission>
您可以在清单文件中设置默认的旋转设置,例如:
<activity android:name=".MainTabActivity" android:screenOrientation="portrait">
</activity>
要通过程序更改方向,您需要调用Activity.setRequestedOrientation()
a1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(getApplicationContext(), MainActivity.class));
android.provider.Settings.System.putInt(getContentResolver(),
android.provider.Settings.System.USER_ROTATION,0);
}
});
a2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(getApplicationContext(), MainActivity.class));
android.provider.Settings.System.putInt(getContentResolver(),
android.provider.Settings.System.USER_ROTATION,90);
}
});