如何以编程方式控制整个设备的屏幕方向并进行更改? 这里链接到一些可以做到这一点的应用程序:
http://dandroidtabletpc.com/download-total-screen-control-1-9-3-full-android-apk.html/
http://ru.androidzoom.com/android_applications/tools/ultimate-rotation-control_bznhn.html
也许有人知道像这样的开源项目?
答案 0 :(得分:12)
@Override
public void onCreate(Bundle savedInstanceState) {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
或
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
答案 1 :(得分:5)
以下是我所知道的两种方法:
如果您希望设置的方向覆盖应用自己的方向偏好设置,请按照How can I globally force screen orientation in Android?中的说明进行操作。
如果您不希望设置的方向覆盖应用自己的方向偏好设置,请首先确保手机的自动旋转已关闭:
Settings.System.putInt(
this.contentResolver,
Settings.System.ACCELEROMETER_ROTATION,
0 //0 means off, 1 means on
);
然后设置手机的用户方向:
Settings.System.putInt(
getContentResolver(),
Settings.System.USER_ROTATION,
Surface.ROTATION_0 //Use any of the Surface.ROTATION_ constants
);
不要忘记检查putInt
的返回值,以确保更改成功。
另外,我认为你需要以下许可:
<uses-permission android:name="android.permission.WRITE_SETTINGS" />
答案 2 :(得分:1)
首先,在android中,如果你没有通过代码或xml文件设置任何方向,那么你的应用程序默认具有方向。
如果您的要求是,尽管手机处于纵向/横向模式并且您需要单个产品,那么您可以通过代码锁定它,如下面的oncreate方法::
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); //or
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
答案 3 :(得分:-1)
使用以下代码,您可以获得活动的当前方向。
int orientation = getResources().getConfiguration().orientation;
if(orientation == Configuration.ORIENTATION_LANDSCAPE)
{
Log.v(TAG,"Configuration.ORIENTATION_LANDSCAPE");
}
else if(orientation == Configuration.ORIENTATION_PORTRAIT)
{
Log.v(TAG, "Configuration.ORIENTATION_PORTRAIT");
}
else if(orientation == Configuration.ORIENTATION_SQUARE)
{
Log.v(TAG, "Configuration.ORIENTATION_SQUARE");
}
else if(orientation == Configuration.ORIENTATION_UNDEFINED)
{
Log.v(TAG, "Configuration.ORIENTATION_UNDEFINED");
}
然后使用以下代码,您可以将活动方向设置为纵向或横向。
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);