2012-03-10 17 views
3

画面の向きをデフォルトのorinetationにロックしたかった。私はこれを達成する上で問題があります。最初は、マニフェストから画面をポートレートにロックしました。ポートレートのデフォルトデバイスでは正常に動作します。しかし、多くのタブレットはデフォルトで風景を持っていますので、これらのデバイスでは縦向きにロックするのは適切ではありません。私はこのデフォルトの向きを検出してロックしたいと思っています。私は風景がデフォルトの向きである場合、私は方向を風景に固定し、その肖像画がポートに固定されたいと思っています。これを行う方法。私はこの部分で立ち往生しています。私は両方向(自動)をサポートしたくありません。助けてください画面の向きを(自然な)デフォルトの向きに固定する

ありがとうございます。

+0

...デフォルトではタブレットには横長がありません。 「デフォルト」はありません。私は何か生産的なやり方をしているとき、そして私が楽しまれている風景の中で、僕は通常肖像画になっています。なぜユーザーの嗜好を尊重したくないのですか? –

+0

http://android-developers.blogspot.in/2010/09/one-screen-turn-deserves-another.html "ポートレイトがデフォルトモードであるとは思わないでください。"多くの錠剤は自然の風景です。 –

+0

私はそれを読んだ。まだ「デフォルト」(名詞)はありません。キーボードが接続された変圧器を持っている人は、確かにデフォルト(動詞)になります。キーボードを切り離します。ユーザーの好み、月の位相など、ユーザーの嗜好を尊重してみませんか? –

答えて

1

さまざまなデバイスのデフォルトの向きがあります。たとえば、Galaxy 10タブレットのデフォルトの向きは、ネクサス7タブレットとは異なります。あなたは、ディスプレイの向きを取得するときは、次の値を取得する:

enter image description here

は、あなたがあなたのロック方法で行う必要があるが、次のされて、そう言った:

public void mLockScreenRotation(FA_Padre actividad){ 
    int buildVersionSDK = Build.VERSION.SDK_INT; 
    Display display = ((WindowManager) actividad.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay(); 
    int orientation=0; 
    if(buildVersionSDK >= 8){ 
     orientation=display.getRotation(); 
    } 
    /****************Phone*************************************/ 
    if(buildVersionSDK < 8){// older Android versions with only two orientations 
     switch (actividad.getResources().getConfiguration().orientation){ 

     case Configuration.ORIENTATION_PORTRAIT: 
      actividad.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); 
      break; 
     case Configuration.ORIENTATION_LANDSCAPE: 
      actividad.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); 
      break; 

     } 
    }else if((buildVersionSDK > 7) && (!GlobalInfo.isTablet())){// Newer Android phones with more than two orientations 

     switch(orientation){ 

     case Surface.ROTATION_0: 
      actividad.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); 
      break; 
     case Surface.ROTATION_90: 
      actividad.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); 
      break; 
     case Surface.ROTATION_180: 
      actividad.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); 
      break; 
     case Surface.ROTATION_270: 
      actividad.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE); 
      break; 

     } 
    /****************TABLET*************************************/ 
    }else{ 
     int width = 0; 
     int height = 0; 
     switch(orientation){ 
      /*If the default orientation of the device is landscape Rotation_0 and rotation_180 will be the case if the device is being held in landscape. if the default orientation of the device is portrait rotation_0 or 180 will only be the case if the device is in portrait mode*/ 
      case Surface.ROTATION_0: 
      case Surface.ROTATION_180: 
       width = display.getWidth(); 
       height = display.getHeight(); 
       break; 
      /*the opposite in here*/ 
      case Surface.ROTATION_90: //  
      case Surface.ROTATION_270: 
       width = display.getHeight(); 
       height = display.getWidth(); 
       break; 
      default: 
       break; 
     } 
     if(width > height){//Default ORIENTATION = LANDSCAPE, lock the screen in the current orientation 
      switch(orientation){ 

      case Surface.ROTATION_0: 
       actividad.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); 
       break; 
      case Surface.ROTATION_90: 
       actividad.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT); 
       break; 
      case Surface.ROTATION_180: 
       actividad.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE); 
       break; 
      case Surface.ROTATION_270: 
       actividad.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); 
       break; 
      } 

     } else {//Default ORIENTATION = PORTRAIT, lock the screen in the current orientation 
      switch(orientation){ 

      case Surface.ROTATION_0: 
       actividad.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); 
       break; 
      case Surface.ROTATION_90: 
       actividad.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); 
       break; 
      case Surface.ROTATION_180: 
       actividad.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT); 
       break; 
      case Surface.ROTATION_270: 
       actividad.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE); 
       break; 
      } 

     } 

    } 
} 
0

Lockorientationactivityをデフォルトにこの1つのロック画面を試してみてください。

public class Lockorientationactivity extends Activity { 
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     int getConf=this.getResources().getConfiguration().orientation; 

     if(getConf==Configuration.ORIENTATION_PORTRAIT) 
     { 
      this.setRequestedOrientation(
       ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); 
      Toast.makeText(getBaseContext(), "ORIENTATION_PORTRAIT", Toast.LENGTH_SHORT).show(); 
     } 
     else 
     { 
      Toast.makeText(getBaseContext(), "ORIENTATION_LANDSCAPE", Toast.LENGTH_SHORT).show(); 
      this.setRequestedOrientation(
        ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); 
     } 

      setContentView(R.layout.main); 
    } 
} 

しかし、あなたは活動のandroid:configChanges="orientation"を設定する必要があります。

+0

これはちょうど私のために働いた方法です。私はそれが非常に適切な解決策だとは思わない。私はちょうど私のマニフェストのアンドロイドにこれを置いています:screenOrientation = "nosensor" 今風景デバイスは風景に特有のレイアウトをとり、ポートレートデバイスはポートレートに特有のレイアウトをとっています。私は "nosensor"を与えているのでプラスの方向はロックされています。しかし、私はいくつかの記事でこれはまだ適切な方法ではないときにソフトキーパッドを開くときのようにうまくいかないかもしれないと読む。私は別の方法を見つけるまで、今のところ行きます。 ありがとうございます。 –

0

画面をロックするにはコードでは、画面の実際の回転(0,90,180,270)を使用する必要があり、スマートフォンではその自然な位置を知る必要があります。自然な位置は肖像画となり、タブレットでは風景になります。

コード(ロックとロック解除の方法)は、一部のデバイス(スマートフォンやタブレット)でテストされています。

public static void lockScreenOrientation(Activity activity) 
{ 
    WindowManager windowManager = (WindowManager) activity.getSystemService(Context.WINDOW_SERVICE); 
    Configuration configuration = activity.getResources().getConfiguration(); 
    int rotation = windowManager.getDefaultDisplay().getRotation(); 

    // Search for the natural position of the device  
    if(configuration.orientation == Configuration.ORIENTATION_LANDSCAPE && 
     (rotation == Surface.ROTATION_0 || rotation == Surface.ROTATION_180) || 
     configuration.orientation == Configuration.ORIENTATION_PORTRAIT && 
     (rotation == Surface.ROTATION_90 || rotation == Surface.ROTATION_270)) 
    { 
     // Natural position is Landscape  
     switch (rotation) 
     { 
      case Surface.ROTATION_0:  
       activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);  
       break;  
      case Surface.ROTATION_90: 
       activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT); 
      break;  
      case Surface.ROTATION_180: 
       activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE); 
       break;   
      case Surface.ROTATION_270: 
       activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); 
       break; 
     } 
    } 
    else 
    { 
     // Natural position is Portrait 
     switch (rotation) 
     { 
      case Surface.ROTATION_0: 
       activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); 
      break; 
      case Surface.ROTATION_90: 
       activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); 
      break; 
      case Surface.ROTATION_180: 
       activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT); 
       break;   
      case Surface.ROTATION_270: 
       activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE); 
       break; 
     } 
    } 
} 

public static void unlockScreenOrientation(Activity activity) 
{ 
    activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED); 
} 
+0

それは私のために働かない。 onCreate内にコピー&ペーストされ、lockScreenOrientationが呼び出されました。 – ARLabs

関連する問題