55

デバイスがデフォルトで縦向きか横向きかを調べる方法はありますか? つまり、デバイスを通常どのように使用するかを意味します。

ほとんどの携帯電話には、通常の使用に縦向きの画面がありますが、それを見つけるためのフラグはありますか?

4

8 に答える 8

175

これは次の方法で実行できます。

ランドスケープ用

if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
    // Do some stuff
}

ポートレート用

if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
    // Do some stuff
}

チェック: Configuration.orientation

于 2011-05-21T11:58:23.517 に答える
3

@Codeversed のおかげで、その優れた回答はほぼ機能しています (ただし、示されているとおりではありません) MainActivity。必要なのは、 の宣言の直後など.enable、ブロックの外側で実行できる場所に移動することだけです。onmyOrientationEventListener

import android.app.Activity;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.util.Log;
import android.view.OrientationEventListener;

public class MainActivity extends Activity
{
    public static boolean PORTRAIT_MODE = true;
    OrientationEventListener myOrientationEventListener ;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        myOrientationEventListener = new OrientationEventListener(this,
                SensorManager.SENSOR_DELAY_NORMAL)
        {
            @Override
            public void onOrientationChanged(int orientation)
            {
                PORTRAIT_MODE = ((orientation < 100) || (orientation > 280));
                Log.w("Orient", orientation + " PORTRAIT_MODE = " + PORTRAIT_MODE);
            }
        };
        Log.w("Listener", " can detect orientation: " + myOrientationEventListener.canDetectOrientation() + " ");
        myOrientationEventListener.enable();
    }
}
于 2016-02-24T20:50:50.953 に答える
3
package com.android.portraitandlandscape;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.Display;
import android.widget.Toast;

public class PortraitLandScape extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    Display display = getWindowManager().getDefaultDisplay(); 
    int width = display.getWidth();
    int height = display.getHeight();

    Log.v("log_tag", "display width is "+ width);
    Log.v("log_tag", "display height is "+ height);

    if(width<height){
        Toast.makeText(getApplicationContext(),"Device is in portrait mode",Toast.LENGTH_LONG ).show();
    }
    else{
        Toast.makeText(getApplicationContext(),"Device is in landscape  mode",Toast.LENGTH_LONG ).show();
    }

 }
}

デバイスが横向きか縦向きかを確認するために、このコードを試すことができます。

于 2011-05-21T11:43:31.973 に答える
2

あなたの活動では、これを行います:

if(getRequestedOrientation() == ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE)

例えば...

于 2014-09-02T05:42:22.367 に答える
1

これが役立つかどうかはわかりませんが、これは私が書いた簡単な例であり、リスナーを作成する方法を示しています...これにより、自分がどの方向にいるのかを理解できるようになります.

...textviewOrientation = (TextView)findViewById(R.id.textView1);

    myOrientationEventListener = new OrientationEventListener(this, 
              SensorManager.SENSOR_DELAY_NORMAL){

        @Override
        public void onOrientationChanged(int arg0) {

         textviewOrientation.setText("Orientation: " + String.valueOf(arg0));
         myOrientationEventListener.enable();


             if ((arg0 < 100) || (arg0 > 280)){

                 setRequestedOrientation(
                     ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

             } else {

                 setRequestedOrientation(
                     ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

             }


        }

    };...


...@Override
protected void onDestroy() {
    // TODO Auto-generated method stub
    super.onDestroy();
    myOrientationEventListener.disable();
}...
于 2011-06-23T02:07:37.803 に答える