【问题标题】:setting a max width for my dialog为我的对话框设置最大宽度
【发布时间】:2011-12-31 00:06:46
【问题描述】:

我的对话框使用线性布局作为其根,并使用以下代码作为其自定义主题:

    <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>

是否可以设置最大宽度?它在手机上很好,但我正在尝试为平板电脑优化它,而且它们太大了。

【问题讨论】:

    标签: android


    【解决方案1】:

    宽度将在线性布局的 XML 中处理,而不是您应用到它的样式。使用 XML 中的 android:layout_width 标记来指定它可能的宽度。

    【讨论】:

      【解决方案2】:

      一个老问题,但没有合适的答案,但你可以在这里找到一些提示:How to customize the width and height when show an Activity as a Dialog

      这有助于自定义宽度和高度,但不会设置最大宽度和高度!为了在使用对话框主题的活动上实现这一点,我必须做几件事:

      1) 设置布局侦听器以了解何时设置对话框布局。

      2) 如果对话框的大小超出所需限制,则调整对话框布局,有效设置最大大小

      这是我目前正在使用的工作 sn-p,在 setContentView() 之后调用:

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

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

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

      【讨论】:

        【解决方案3】:

        是的,如果你想为你的 xml 使用 Widht GO 并尝试做

        android:layout_width="Defined in pixels//20px"
        

        或者你也可以试试这个

        android:width="Defined in pixels// 30px"
        

        希望对你有帮助

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2018-02-05
          • 2018-05-08
          • 2018-12-18
          • 1970-01-01
          • 2011-04-11
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多