设置为我对话的最大宽度 [英] setting a max width for my dialog

查看:143
本文介绍了设置为我对话的最大宽度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的对话框使用的LinearLayout其根源,其使用以下code作为它的自定义主题:

My dialog is using a linearlayout for its root, and its using the following code as its custom theme:

    <style name="HuskyBusDialog">
    <item name="android:windowNoTitle">true</item>
    <item name="android:windowFrame">@null</item>
    <item name="android:windowBackground">@drawable/panel_background</item>
    <item name="android:windowIsFloating">true</item>
    <item name="android:windowContentOverlay">@null</item>
    <item name="android:windowAnimationStyle">@android:style/Animation.Dialog</item>
    <item name="android:windowSoftInputMode">stateUnspecified|adjustPan</item>
    <item name="android:colorBackgroundCacheHint">@null</item> 
</style>

是否可以设置一个最大宽度?它在手机上,但我尝试优化它的平板电脑好人,他们是太大了。

Is it possible to set a max width? Its fine on phones but im trying to optimize it for tablets and they are too big.

推荐答案

这是老问题,但没有答复是合适的,但你可以在这里找到一些提示:<一href=\"http://stackoverflow.com/questions/6624480/how-to-customize-the-width-and-height-when-show-an-activity-as-a-dialog\">How显示一个活动作为一个对话框时,定制的宽度和高度

An old question, however no replies are suitable, but you can find some hint here: How to customize the width and height when show an Activity as a Dialog

这有助于定制的宽度和高度,而不是设置最大宽度和高度!为了实现这一目标上使用对话的主题活动,我不得不做两件事情:

This helps customize the width and height, but not set the max width and height! To achieve that on an activity using a dialog theme, I had to do a couple of things:

1)设定的布局听者时对话框布局被设置为知道

1) set a layout listener to know when the dialog layout is set.

2)调整对话框布局,如果其大小超出了期望的极限,有效地设置最大尺寸

2) Adjust the dialog layout if its size was beyond the desired limit, effectively setting max size

下面是我目前使用的工作片段,的setContentView()之后调用:

Here is the working snippet I'm currently using, called after setContentView():

    getWindow().getDecorView().getViewTreeObserver().addOnGlobalLayoutListener(
            new OnGlobalLayoutListener()
    {
        @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
        @SuppressWarnings("deprecation")
        @Override
        public void onGlobalLayout()
        {
            adjustDialog();
        }
    });

现在对话框的大小调整,实际屏幕尺寸的百分比:

Now the dialog size adjustment, as a percentage of actual screen size:

private void adjustDialog()
{
    Window w = getWindow();
    w.setGravity(Gravity.CENTER);

    int current_width = w.getDecorView().getWidth();
    int current_height = w.getDecorView().getHeight();

    WindowManager.LayoutParams lp = w.getAttributes();

    DisplayMetrics dm = getResources().getDisplayMetrics();
    int max_width = (int) (dm.widthPixels * 0.90);
    int max_height = (int) (dm.heightPixels * 0.90);

    if (current_width > max_width)
    {
        lp.width = max_width;
    }
    if (current_height > max_height)
    {
        lp.height = max_height;
    }

    w.setAttributes(lp);
}

这篇关于设置为我对话的最大宽度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆