23
A
回答
45
這應該做的伎倆爲您提供:
import android.provider.Settings;
public static void setAutoOrientationEnabled(Context context, boolean enabled)
{
Settings.System.putInt(context.getContentResolver(), Settings.System.ACCELEROMETER_ROTATION, enabled ? 1 : 0);
}
添加許可權的AndroidManifest.xml
<uses-permission android:name="android.permission.WRITE_SETTINGS" />
您可以找到的文檔here
2
始終使用用戶指定的屏幕方向,這將應用用戶選擇的任何方向,並且如果其禁用則不會旋轉屏幕。
activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_USER);
1
這是我對這個問題的障礙。我必須實現一個按鈕,其功能與設置菜單中的鎖定按鈕相同。
可以使用setRotationScreenFromSettings解決您的問題
public static boolean getRotationScreenFromSettingsIsEnabled(Context context)
{
int result = 0;
try
{
result = Settings.System.getInt(context.getContentResolver(), Settings.System.ACCELEROMETER_ROTATION);
}
catch (Settings.SettingNotFoundException e)
{
e.printStackTrace();
}
return result == 1;
}
public static void setRotationScreenFromSettings(Context context, boolean enabled)
{
try
{
if (Settings.System.getInt(context.getContentResolver(), Settings.System.ACCELEROMETER_ROTATION) == 1)
{
Display defaultDisplay = ((WindowManager) context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
Settings.System.putInt(context.getContentResolver(), Settings.System.USER_ROTATION, defaultDisplay.getRotation());
Settings.System.putInt(context.getContentResolver(), Settings.System.ACCELEROMETER_ROTATION, 0);
}
else
{
Settings.System.putInt(context.getContentResolver(), Settings.System.ACCELEROMETER_ROTATION, 1);
}
Settings.System.putInt(context.getContentResolver(), Settings.System.ACCELEROMETER_ROTATION, enabled ? 1 : 0);
}
catch (Settings.SettingNotFoundException e)
{
e.printStackTrace();
}
}
private void regirsterLockScreenOrientationChangedListner()
{
ContentObserver rotationObserver = new ContentObserver(new Handler())
{
@Override
public void onChange(boolean selfChange)
{
refreshLockScreenOrientationBtn();
}
};
context.getContentResolver().registerContentObserver(Settings.System.getUriFor(Settings.System.ACCELEROMETER_ROTATION),
true, rotationObserver);
}`
添加權限在AndroidManifest.xml中
<uses-permission android:name="android.permission.WRITE_SETTINGS" />
1
你必須使用下面的代碼裏面的onCreate()函數來解決它。工作...
setRequestedOrientation (ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
相關問題
- 1. 以編程方式啓用/禁用Log4jLogger?
- 2. 以編程方式啓用/禁用鍵盤聲音和振動
- 3. 在macOS中以編程方式啓用,禁用和啓動服務
- 4. 如何以編程方式啓用/禁用移動數據
- 5. 以編程方式啓用和禁用某些@ RabbitListener的Spring?
- 6. 以編程方式禁用gif動畫
- 7. 以編程方式禁用ScollViewer滾動
- 8. 以編程方式自動旋轉鎖定
- 9. 以編程方式啓用/禁用沉浸模式
- 10. 以編程方式旋轉UIViewController?
- 11. 啓動後禁用屏幕旋轉?
- 12. 如何以編程方式啓用和禁用USB在Android應用程序
- 13. Magento - 以編程方式禁用自動索引
- 14. 以編程方式禁用Gboard上的自動建議
- 15. 以編程方式禁用自動佈局約束
- 16. 如何以編程方式啓用/禁用Windows功能
- 17. 在Windows 7上以編程方式啓用/禁用連接
- 18. 以編程方式啓用/禁用實體代理
- 19. 以編程方式啓用/禁用藍牙配置文件
- 20. 如何以編程方式啓用/禁用Azure功能
- 21. 如何以編程方式啓用/禁用IE代理設置?
- 22. 如何在rooted android上以編程方式禁用/啓用gps?
- 23. 是否可以通過編程方式啓用/禁用硬件?
- 24. Com端口以編程方式禁用/啓用C#
- 25. 以編程方式啓用/禁用設備的Win32 API函數
- 26. 以編程方式啓用/禁用ASP.NET會話狀態?
- 27. 你如何以編程方式禁用ListView和滾動ListView?
- 28. 如何禁用任何自動旋轉?
- 29. 在Cocos2d中禁用自動旋轉
- 30. 禁用UIView的自動旋轉
這似乎工作,但它鎖定你鎖定橫向方向,即使我在肖像。我正在測試蜂巢片,所以我不確定是否有區別....這是預期的行爲嗎? – christoff 2011-06-29 20:31:31