0

方向改变时如何改变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))));

我想在方向改变时做同样的事情。请帮忙

4

2 回答 2

1

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

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));
            }
于 2013-05-10T10:57:26.877 回答
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 上这样做。

于 2013-05-10T10:55:35.150 回答