31
A
回答
2
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();
}
}
}
您可以尝试使用此代码来检查设备是横向还是纵向。
116
你可以这样做:
对于Lanscape
if(getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE){
//Do some stuff
}
肖像
if(getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT){
//Do some stuff
}
检查:http://developer.android.com/reference/android/content/res/Configuration.html#orientation
0
我不知道,如果这将有助于但是这是一个简单的例子,我写,会告诉你如何有一个监听器...,这将让你弄清楚你是在什么方向
...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);
}
}
};...
[email protected]
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
myOrientationEventListener.disable();
}...
2
在你actvity,做到这一点:
if(getRequestedOrientation() == ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE)
例如...
1
感谢@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();
}
}
1
我有类似的问题。通过比较旋转和方向的值来解决这个问题。无需使用传感器或任何监听器,只要您希望就拨打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;
}
}
}
相关问题
- 1. 如何在Android中检查图像是纵向还是横向?
- 2. 如何知道图片是横向还是纵向?
- 3. 如何找出纵向或横向的屏幕方向?
- 4. Android的横向/纵向变化difficults
- 5. Android:如何处理纵向反转纵向和横向反转横向事件
- 6. iPad的UIViewController加载为纵向时,设备在横向
- 7. Android:如何强制整个Android设备仅在横向或纵向上运行
- 8. 尽管窗口是横向的,但纵向方向的iAd横幅显示
- 9. popViewControllerAnimated:从横向到纵向
- 10. iphone:从纵向到横向
- 11. 维持横向/纵向
- 12. NavigationController横向和纵向
- 13. UIImagePickerController摄像头预览是横向应用中的纵向
- 14. 如何在Android中检查照片是以横向还是纵向模式拍摄的?
- 15. 查找设备是否指向正确的标题/方向?
- 16. 在横向和纵向模式下使用ScrollViewer正确滚动
- 17. 是否可以在纵向设计的视图中设计横向项目?
- 18. iPad应用程序是否必须支持横向和纵向?
- 19. 如何判断照片是以横向还是纵向拍摄的? JPEG .NET元数据方向
- 20. 如何以横向适应纵向图像在纵横比保持纵横比
- 21. Android切换VideoView方向从纵向到横向而不横向拉伸视频
- 22. Android XML布局适用于横向广告,但不是正常方向
- 23. jQuery的横向和纵向定位脚本不是IE8
- 24. 方向纵向和横向模式
- 25. Android输入法编辑器适用于横向但不是纵向
- 26. 使用CSS移动查询禁用横向或纵向方向
- 27. 如何设置纵向和横向的单个应用程序
- 28. In App Purchase服务器模型是面向设备还是面向用户的?
- 29. 加载不同的纵向/横向UIViews
- 30. 横向和纵向上的UIActionSheet位置
您的意思是查找应用程序运行时检查天气设备是横向还是纵向? – Herry 2011-05-21 11:22:32
[如何在Android上检查设备自然(默认)方向(例如,获取横向,例如,Motorola Charm或Flipout)](http://stackoverflow.com/questions/4553650/how-to-check-device- natural-default-orientation-on-android-ie-get-landscape) – weston 2014-05-13 11:24:15