【问题标题】:Rotate Dialog and AlertDialog other than fixed device orientation旋转 Dialog 和 AlertDialog 而非固定设备方向
【发布时间】:2016-11-25 12:11:14
【问题描述】:

我尝试使用搜索,但没有找到解决此问题的方法。 我有一个固定横向方向的相机应用程序。一个orientationEventListener 正确地旋转了我所有的按钮。但是,有些按钮会显示一个对话框或 alertdialog,并且这些对话框不会旋转。这是 MainActivity 中的相关缩短代码

button1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            final Dialog dialog = new Dialog(CameraActivity.this);
            dialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
            dialog.setContentView(R.layout.layout_dialog_mode);
            // set up texts in layout_dialog_mode

private void createMenu() {
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    final String[] modeS = {getString(R.string.mode_1), getString(R.string.mode_2), getString(R.string.mode_3), getString(R.string.mode_4), getString(R.string.mode_5)};
    builder.setTitle(getString(R.string.mode_title));
    builder.setSingleChoiceItems(modeS, mode, new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int item) {
             // set things as chosen by user
            }
            dialog.dismiss();  /* close menu after selection    */
        }
    });

    builder.show();
}

layout_dialog_mode 是一个包含 TextViews 和 ImageViews 的线性布局。

按钮的旋转是这样的:

private void initialiseOEListener() {

    oEListener = new OrientationEventListener(this, SensorManager.SENSOR_DELAY_NORMAL) {
        @Override
        public void onOrientationChanged(int orientation) {
            try {
                if (orientation == ORIENTATION_UNKNOWN) return;

                orientation = (orientation + 45) / 90 * 90;

                switch (orientation) {
                   // set params and orientation
                }

                ImageButton ibu = (ImageButton) findViewById(R.id.button0);
                ibu.setRotation(orientation);
                ibu = (ImageButton) findViewById(R.id.button1);
                ibu.setRotation(orientation);
                // and so on

            } catch (Exception e) {


            }
        }
    };

现在我的问题是:是否可以以与按钮相同的方式旋转对话框?目前,它们以横向显示,因为我将此 Activity 的设备方向设置为横向。例如,当用户将手机旋转到纵向并单击 button1 时,对话框将显示错误,因为它没有旋转。

非常感谢

【问题讨论】:

    标签: android rotation orientation


    【解决方案1】:

    好的,我通过为每个对话框创建单独的 Activity 解决了这个问题。现在的问题是,当新的前台对话框 Activity 旋转时,后台 Activity 也会旋转!我可以在旋转前台 Activity 时以某种方式设置后台 Activity 方向吗?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-02-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多