方向更改时更改自定义对话框的尺寸

时间:2013-05-10 10:34:45

标签: android

当方向改变时,如何在android中更改Dialog的维度。

onCreate中的

代码

static final float[] DIMENSIONS_DIFF_LANDSCAPE = {20, 60};
static final float[] DIMENSIONS_DIFF_PORTRAIT = {40, 60};

final float scale = getContext().getResources().getDisplayMetrics().density;
int orientation = getContext().getResources().getConfiguration().orientation;

float[] dimensions =
                (orientation == Configuration.ORIENTATION_LANDSCAPE)
                        ? DIMENSIONS_DIFF_LANDSCAPE : DIMENSIONS_DIFF_PORTRAIT;
                 addContentView(mContent, new LinearLayout.LayoutParams(
                    display.getWidth() - ((int) (dimensions[0] * scale + 0.5f)),
                    display.getHeight() - ((int) (dimensions[1] * scale + 0.5f))));

我希望在方向改变时也这样做。请帮助

2 个答案:

答案 0 :(得分:1)

只需根据oriebtation检查它是一个自定义代码来设置对话框高度:它通过实现我的对话框变得灵活...我在平板电脑中使用它...

WindowManager winMan = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
            int height = winMan.getDefaultDisplay().getHeight();
            int width=winMan.getDefaultDisplay().getWidth();
            if(height>width){

                if(DeviceType.getDeviceType(context)==Device.NEXUS7){
                dialoglayout.setLayoutParams(new FrameLayout.LayoutParams(700,1100));
                }else if (DeviceType.getDeviceType(context)==Device.GALAXYTAB2){
                    dialoglayout.setLayoutParams(new FrameLayout.LayoutParams(580,900));
                }else if(DeviceType.getDeviceType(context)==Device.GALAXY10){
                    dialoglayout.setLayoutParams(new FrameLayout.LayoutParams(700,1100));
                }


            else{
                if(DeviceType.getDeviceType(context)==Device.NEXUS7){
                    dialoglayout.setLayoutParams(new FrameLayout.LayoutParams(700,700));
                }else if (DeviceType.getDeviceType(context)==Device.GALAXYTAB2){
                    dialoglayout.setLayoutParams(new FrameLayout.LayoutParams(700,500));
                    System.out.println("galaxy tab 2 landescape");
                }else if(DeviceType.getDeviceType(context)==Device.GALAXY10){
                dialoglayout.setLayoutParams(new FrameLayout.LayoutParams(700,700));
            }

答案 1 :(得分:0)

我建议您创建一个方法init,并在onCreate和方向更改期间放置您想要发生的内容。

首先,您应该将此添加到您的活动xml

android:configChanges="keyboardHidden|orientation"

然后实现方法

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);

    static final float[] DIMENSIONS_DIFF_LANDSCAPE = {20, 60};
    static final float[] DIMENSIONS_DIFF_PORTRAIT = {40, 60};

    final float scale = getContext().getResources().getDisplayMetrics().density;

    float[] dimensions =
                (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE)
                        ? DIMENSIONS_DIFF_LANDSCAPE : DIMENSIONS_DIFF_PORTRAIT;
                 addContentView(mContent, new LinearLayout.LayoutParams(
                    display.getWidth() - ((int) (dimensions[0] * scale + 0.5f)),
                    display.getHeight() - ((int) (dimensions[1] * scale + 0.5f))));
}

但你也应该在onCreate上做到这一点。