デバイスがデフォルトで縦向きか横向きかを調べる方法はありますか? つまり、デバイスを通常どのように使用するかを意味します。
ほとんどの携帯電話には、通常の使用に縦向きの画面がありますが、それを見つけるためのフラグはありますか?
これは次の方法で実行できます。
ランドスケープ用
if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
// Do some stuff
}
ポートレート用
if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
// Do some stuff
}
@Codeversed のおかげで、その優れた回答はほぼ機能しています (ただし、示されているとおりではありません) MainActivity
。必要なのは、 の宣言の直後など.enable
、ブロックの外側で実行できる場所に移動することだけです。on
myOrientationEventListener
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();
}
}
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();
}
}
}
デバイスが横向きか縦向きかを確認するために、このコードを試すことができます。
あなたの活動では、これを行います:
if(getRequestedOrientation() == ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE)
例えば...
これが役立つかどうかはわかりませんが、これは私が書いた簡単な例であり、リスナーを作成する方法を示しています...これにより、自分がどの方向にいるのかを理解できるようになります.
...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();
}...