有没有办法通过默认方式查找设备是横屏还是竖屏?我的意思是您通常使用设备的方式。
大多数手机在正常使用时都具有纵向屏幕,但是否有一些标记可以找出这一点?
你可以通过以下方式实现:
针对横屏模式
if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
// Do some stuff
}
针对纵向排版
if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
// Do some stuff
}
这很简单,只需要一个If-Else
块:
if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
// landscape
} else {
// portrait
}
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)
android:screenOrientation="portrait|landscape"
,则此方法将无法正常工作。 - Pierre我不确定这是否有帮助,但我写了一个快速的例子,将向您展示如何拥有监听器......它将允许您确定您所处的方向。
...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();
}...
isDefaultLandscape
方法即可。private boolean isDefaultLandscape(final Context context)
{
Display display = ((WindowManager) context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
int rotation = display.getRotation();
int orientation = context.getResources().getConfiguration().orientation;
switch (rotation)
{
case Surface.ROTATION_180:
case Surface.ROTATION_0:
{
return orientation == Configuration.ORIENTATION_LANDSCAPE;
}
default:
{
return orientation == Configuration.ORIENTATION_PORTRAIT;
}
}
}
// Current orientation
public boolean landscape = false;
public boolean isLandscape(){
DisplayMetrics displaymetrics = new DisplayMetrics();
context.getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
int width = displaymetrics.widthPixels;
int height = displaymetrics.heightPixels;
if(width<height){
landscape = false;
}
else{
landscape = true;
}
return landscape;
}
private boolean isPortraitMode() {
int orientation = context.getResources().getConfiguration().orientation;
if (orientation == Configuration.ORIENTATION_LANDSCAPE) {
return false;
}
if (orientation == Configuration.ORIENTATION_PORTRAIT) {
return true;
}
Log.d(TAG, "isPortraitMode returning false by default");
return false;
}