如何限制平板电脑屏幕方向
我想限制平板电脑在特定屏幕方向(如横向)上启动?我在这里关于整个平板电脑而不是特定的应用程序,并且我确定我没有考虑在平板电脑启动后锁定自动旋转,我正在考虑限制平板电脑在风景/写真。如何限制平板电脑屏幕方向
找到了答案,一个应用程序将只运行一次。
创建一个应用程序,并给它的清单中添加一个权限write_settings & 听开机的:
<uses-permission android:name="android.permission.WRITE_SETTINGS" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
然后做出receiver
类扩展BroadcastReceivertype
,与fowlloing权限:
<receiver android:enabled="true" android:exported="true"
android:name="com.example.BootCompletedReceiver"
android:permission="android.permission.RECEIVE_BOOT_COMPLETED">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.QUICKBOOT_POWERON" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
然后在在receiver
类方法:
ContentResolver contentResolver = context.getContentResolver();
//To handle device rotation
Settings.System.putInt(contentResolver, Settings.System.ACCELEROMETER_ROTATION, 0);
Settings.System.putInt(contentResolver, Settings.System.USER_ROTATION, Surface.ROTATION_90); // 0 for default, then 90, 180, 270
然后运行这个程序只是一个时间来写权限到系统中,然后你就不再需要它。
import android.provider.Settings;
public static void setAutoOrientationEnabled(ContentResolver resolver, boolean enabled)
{
Settings.System.putInt(context.getContentResolver(), Settings.System.ACCELEROMETER_ROTATION, enabled ? 1 : 0);
}
//adding permission
<uses-permission android:name="android.permission.WRITE_SETTINGS" />
它不是一个应用程序级别的问题,这是一个平板电脑级别的问题 – 2014-09-24 10:39:42
你需要自动旋转屏幕平板? – 2014-09-24 10:41:18
写入此代码。 – 2014-09-24 10:45:14
创建文件orientationLock.xml
,并把这个在它 - res/values/orientationLock.xml
(名称并不重要) -
<bool name="orien_lock">true</bool>
现在创建一个文件夹(如果你还没有拥有它)命名values-sw600dp
和values-xlarge
在资源,并把这个在它 -
<bool name="orien_lock">false</bool>
然后,把这个你活动的onCreate方法:
要锁定在纵向模式
if(getResources().getBoolean(R.bool.orien_lock)){
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
要锁定在横向模式
if(getResources().getBoolean(R.bool.orien_lock)){
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
}
说明
在在宽度超过为600dp设备( mini标签)或x-large(在Android 3.2之前),orien_lock的值将变为false(由我们定义),因为values-sw600dp
中的文件将被调用,并且它将被锁定在横向/纵向模式中。
你指的是什么?res/values /? '/ system'? – 2014-09-24 11:19:52
@MuhammedRefaat,我建议你在问问题前浏览API指南 - http://developer.android.com/guide/index.html – Confuse 2014-09-24 14:58:32
对于平板电脑中的每项活动,您都可以在清单文件中使用它 我正在执行类似的任务,其工作非常简单。
<activity
android:name="com.example.abc.MainActivity"
android:label=""
android:screenOrientation="landscape"
android:windowSoftInputMode="adjustResize|adjustPan" >
</activity>
但是这将禁用所有设备的轮换 – Confuse 2014-09-24 10:49:15
你的意思是每个活动在我的平板电脑 – 2014-09-24 10:57:20
我的意思是对于用户可见的每个布局 – 2014-09-24 10:59:05
为此,您应该将'android:screenOrientation =“landscape”'属性设置为您的'应用程序'或'活动' – 2014-09-24 10:30:31
谢谢,但我没有关注应用程序或活动,我正在询问整个平板电脑方向 – 2014-09-24 10:33:29
@MR我不认为有办法... – 2014-09-24 10:36:22