【问题标题】:android screen orientation安卓屏幕方向
【发布时间】:2011-06-09 12:25:26
【问题描述】:

我尝试getOrientation() 获取方向值,但它总是返回 0!

【问题讨论】:

  • 尝试先旋转设备。
  • 回滚了第 3 方标题编辑,因为问题(甚至更多的是答案)关注方向作为一个普遍问题,而不是特别是一种方法。

标签: android orientation


【解决方案1】:

getOrientation() 已弃用,但这不一定是问题的根源。确实,您应该使用 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 将不会向您的 Activity 报告方向更改。需要明确的是:它不会重新启动活动。就我而言,就像你描述的那样,我只得到 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"

效果相同,但这次仅针对您的活动。如果您制作了小型测试程序,这种可能性就会被消除(这就是我推荐它的原因)。

备注:是的,整个方向/旋转主题确实是一个“有趣”的主题。尝试一下,使用 Log.d(),实验,学习。

【讨论】:

    【解决方案2】:

    你应该使用:

    getResources().getConfiguration().orientation
    

    它可以是ORIENTATION_LANDSCAPE、ORIENTATION_PORTRAIT 之一。

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

    【讨论】:

    • 如何获得更详细的方向,例如横向左/右?
    【解决方案3】:

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

    getResources().getConfiguration().orientation

    Developer Documentation

    公共 int 方向
    自:API 级别 1

    屏幕的整体方向。可能是 ORIENTATION_LANDSCAPE、ORIENTATION_PORTRAIT 或 ORIENTATION_SQUARE 之一。

    【讨论】:

    • 当您需要支持旧操作系统版本时的好解决方案。似乎比getOrientation()更可靠。
    • 到目前为止,这是唯一对我有效的解决方案!
    • 如果我在加载活动后没有旋转屏幕,似乎 getOrientation() 总是返回 ORIENTATION_UNDEFINED。这样就避免了这个问题。
    【解决方案4】:

    为避免使用已弃用的方法,请使用找到的 Android 配置类 here。它从 API lvl 1 开始工作,并且仍然适用于最新的 android 设备。 (未弃用)。

    作为示例,请考虑以下代码 sn-p:

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

    祝你好运-希望这个答案对遇到它的人有所帮助。

    【讨论】:

    • 为什么要通过代码手动切换?布局配置就是为此而生的。 /res/layout 和 /res/layout-land。
    【解决方案5】:

    我认为您需要创建两个不同的布局并在不同的方向上膨胀不同的布局。我给你一个对我来说很好的示例代码。 在自定义适配器的情况下,您可以从您的活动中传递“上下文”。 如果您使用的是自定义适配器,那么您可以尝试以下代码:

    @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);
            }
    

    【讨论】:

      【解决方案6】:

      我在 NexusOne SDK 2.3 上遇到了同样的问题。

      这解决了它:Check orientation on Android phone。

      【讨论】:

        【解决方案7】:
        /* 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();
        

        【讨论】:

          【解决方案8】:

          getOrientation() 已弃用。相反,请尝试getRotation()。

          【讨论】:

          • 知道为什么不推荐使用 getOrientation 吗?据我所知,getRotation 的作用完全相同。
          • 如果你必须支持 API 7,你不能使用getRotation,它是 API 8 以上的。
          猜你喜欢
          • 2011-06-25
          • 2011-06-28
          • 2017-03-24
          • 2011-09-08
          • 2013-04-27
          • 2013-03-29
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多