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