Android的屏幕方向Android的屏幕方向(android screen orientatio

2019-05-12 01:47发布

我试图getOrientation()来获取定位值,但它总是返回0!

Answer 1:

getOrientation()已经过时,但是这不是你的neccesarily问题的根源。 这是事实,你应该使用getRotation()代替getOrientation(),但你只能如果您的目标的Android 2.2(API等级8级)或更高的使用它。 有些人甚至有时的Google似乎忘记了。

举个例子,在我的HTC渴望运行Android 2.2。 getOrientation()getRotation()都报告了相同的值:

  • 0(默认值,肖像)
  • 1(装置90度逆时针),
  • 3(器件90度顺时针)

当你把它“发挥得淋漓尽致”这不报告(旋转180,这将是价值2)。 这个结果可能是特定于设备的。

首先,如果您使用的仿真器或设备,你应该清楚。 你知道如何旋转模拟器? 然后,我会建议使用这样的onCreate()方法创建一个小的测试程序:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    WindowManager mWindowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
    Display mDisplay = mWindowManager.getDefaultDisplay();

    Log.d("ORIENTATION_TEST", "getOrientation(): " + mDisplay.getOrientation());

}

检查您的设备的屏幕已被锁定在设备设置中 设置>显示>自动旋转屏幕 。 如果该复选框未被选中,Android将不会报告定向改变你的活动。 需要明确的是:它不会重新启动该活动。 在我来说,我只得到0,就像你描述。

你可以从你的程序,如果你将这些行添加到的onCreate检查这个()

int sysAutoRotate = 0;
try {
        sysAutoRotate = Settings.System.getInt(getContentResolver(), Settings.System.ACCELEROMETER_ROTATION);
    } catch (SettingNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } 
Log.d("ORIENTATION_TEST", "Auto-rotate Screen from Device Settings:" + sysAutoRotate);

它将返回0,如果自动旋转是关闭的,1,如果自动旋转开启。 另一种尝试。 在原来的计划可能会在清单中已设置

android:screenOrientation="portrait"

同样的效果,但这次只有你的活动。 如果你做的小测试程序,这个possibilty会被淘汰(这就是为什么我建议它)。

备注:是全方向/旋转话题是一个“有趣”的话题确实如此。 尝试的东西出来,用Log.d(),实验,学习。



Answer 2:

如果你想知道,如果当前显示的内容是在横向模式或纵向(可能完全独立于手机的物理旋转),可以使用:

getResources().getConfiguration().orientation

开发者文档

公众诠释取向
自:API级别1

屏幕的总方向。 可能ORIENTATION_LANDSCAPE,ORIENTATION_PORTRAIT,或ORIENTATION_SQUARE之一。



Answer 3:

getOrientation()已过时。 相反,尝试getRotation()



Answer 4:

为了避免使用过时的方法使用发现Android的配置类在这里 。 它的工作原理,因为API拉特1,仍然适用于最新的Android设备。 (不建议使用)。

作为与例如,考虑下面的代码片段:

Configuration config = getResources().getConfiguration();
if (config.orientation == Configuration.ORIENTATION_PORTRAIT)
{
    setContentView(R.layout.portraitStart);
}
else
{
    setContentView(R.layout.landscapeStart);
}

最佳luck-希望这个答案可以帮助谁跑了进去。



Answer 5:

您应该使用:

getResources().getConfiguration().orientation

它可以是一个ORIENTATION_LANDSCAPEORIENTATION_PORTRAIT

http://developer.android.com/reference/android/content/res/Configuration.html#orientation



Answer 6:

/* First, get the Display from the WindowManager */
Display display = ((WindowManager) getSystemService(WINDOW_SERVICE)).getDefaultDisplay();

/* Now we can retrieve all display-related infos */
int width = display.getWidth();
int height = display.getHeight();
int orientation = display.getOrientation();


Answer 7:

我认为你需要创建两个不同的布局,并在不同的方向膨胀不同的布局。 我给你这是工作的罚款,我一个示例代码。 “上下文”你可以从你的活动中自定义适配器的情况下通过。 如果您正在使用自定义适配器,那么你可以试试下面的代码:

@Override
public View getView(final int position,
                    View convertView,
                    ViewGroup parent)
{
    View rowView = convertView;
    ViewHolder holder = null;
    int i = context.getResources().getConfiguration().orientation;

    if (rowView == null) {
        LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        if (i == Configuration.ORIENTATION_PORTRAIT) {
            rowView = inflater.inflate(R.layout.protrait_layout, null, false);
        } else {
            rowView = inflater.inflate(R.layout.landscape_layout, null, false);
        }

        holder = new ViewHolder();
        holder.button = (Button) rowView.findViewById(R.id.button1);
        rowView.setTag(holder);

    } else {
        holder = (ViewHolder) rowView.getTag();
    }
    return rowView;
}

在你的代码中的其他聪明人,你可以直接设置两个不同的布局

    int i = context.getResources().getConfiguration().orientation;

        if (i == Configuration.ORIENTATION_PORTRAIT) {
                    ArrayAdapter<String> adt = new ArrayAdapter<String>(getApplicationContext(), R.layout.protrait_layout, name);
        } else {
                    ArrayAdapter<String> adt = new ArrayAdapter<String>(getApplicationContext(), R.layout.landscape_layout, name);
        }


Answer 8:

我对Nexus One的SDK 2.3同样的问题。

这解决了这个问题: 检查Android手机上的方向 。



文章来源: android screen orientation