2014-09-24 101 views
0

我想限制平板電腦在特定屏幕方向(如橫向)上啓動?我在這裏關於整個平板電腦而不是特定的應用程序,並且我確定我沒有考慮在平板電腦啓動後鎖定自動旋轉,我正在考慮限制平板電腦在風景/寫真。如何限制平板電腦屏幕方向

+0

爲此,您應該將'android:screenOrientation =「landscape」'屬性設置爲您的'應用程序'或'活動' – 2014-09-24 10:30:31

+0

謝謝,但我沒有關注應用程序或活動,我正在詢問整個平板電腦方向 – 2014-09-24 10:33:29

+0

@MR我不認爲有辦法... – 2014-09-24 10:36:22

回答

0

找到了答案,一個應用程序將只運行一次。

創建一個應用程序,並給它的清單中添加一個權限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 

然後運行這個程序只是一個時間來寫權限到系統中,然後你就不再需要它。

0
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" /> 
+0

它不是一個應用程序級別的問題,這是一個平板電腦級別的問題 – 2014-09-24 10:39:42

+0

你需要自動旋轉屏幕平板? – 2014-09-24 10:41:18

+0

寫入此代碼。 – 2014-09-24 10:45:14

0

創建文件orientationLock.xml,並把這個在它 - res/values/orientationLock.xml(名稱並不重要) -

<bool name="orien_lock">true</bool> 

現在創建一個文件夾(如果你還沒有擁有它)命名values-sw600dpvalues-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中的文件將被調用,並且它將被鎖定在橫向/縱向模式中。

+0

你指的是什麼?res/values /? '/ system'? – 2014-09-24 11:19:52

+0

@MuhammedRefaat,我建議你在問問題前瀏覽API指南 - http://developer.android.com/guide/index.html – Confuse 2014-09-24 14:58:32

0

對於平板電腦中的每項活動,您都可以在清單文件中使用它 我正在執行類似的任務,其工作非常簡單。

<activity 
      android:name="com.example.abc.MainActivity" 
      android:label="" 
      android:screenOrientation="landscape" 
      android:windowSoftInputMode="adjustResize|adjustPan" > 
     </activity> 
+0

但是這將禁用所有設備的輪換 – Confuse 2014-09-24 10:49:15

+0

你的意思是每個活動在我的平板電腦 – 2014-09-24 10:57:20

+0

我的意思是對於用戶可見的每個佈局 – 2014-09-24 10:59:05

相關問題