【问题标题】:set width of custom dialog to wrap content android设置自定义对话框的宽度以包装内容android
【发布时间】:2014-02-11 02:11:15
【问题描述】:

我想设置自定义对话框的宽度来包裹内容 但它总是填满屏幕的所有宽度

我已经测试过了

android.view.WindowManager.LayoutParams params = mydialog.getWindow().getAttributes();
params.width = android.view.WindowManager.LayoutParams.WRAP_CONTENT;
mydialog.getWindow().setAttributes((android.view.WindowManager.LayoutParams) params);

还有那个

mydialog.getWindow().setLayout(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);

【问题讨论】:

    标签: android dialog width


    【解决方案1】:

    使用以下属性将样式设置为自定义对话框:

    <style name="CustomDialogTheme" parent="@android:style/Theme.Dialog">
    
        <item name="android:windowIsFloating">false</item>
    
    </style>
    

    【讨论】:

      【解决方案2】:

      我也很难解决这个问题,终于找到了更好的方法来解决这个问题。

      这是解决方法。在你的代码中

      mydialog.getWindow().setLayout(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); 
      

      这不起作用,因为 ProgressDialog 的默认布局具有 match_parent 左右边距。要调整 ProgressDialog 的大小,您需要一个自定义视图,如下所示:

      <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
                    android:background="@android:drawable/dialog_holo_light_frame"
                    android:layout_gravity="center_horizontal"
                    android:id="@+id/container"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content">
      
          <ProgressBar
                  android:layout_marginTop="20dp"
                  android:layout_marginBottom="20dp"
                  android:layout_marginLeft="30dp"
                  android:layout_width="wrap_content"
                  android:layout_height="match_parent"
                  android:id="@+id/progressBar"/>
      
          <TextView
                  android:layout_marginTop="20dp"
                  android:layout_marginBottom="20dp"
                  android:layout_marginRight="30dp"
                  android:layout_width="wrap_content"
                  android:layout_height="match_parent"
                  android:layout_marginLeft="20dp"
                  android:gravity="center"
                  android:id="@+id/message"/>
      </LinearLayout>
      

      并在显示 ProgressDialog 后对其进行充气。自定义视图膨胀后,通过 GlobalLayoutListener 获取自定义视图的宽度,并设置布局宽度。

      mDialog.show();
      
      final Window window = mDialog.getWindow();
      window.setContentView(R.layout.custom_progress_dialog); // this is the above code
      
      final LinearLayout dialogContainer = (LinearLayout) window.findViewById(R.id.container);
      TextView message = (TextView) window.findViewById(R.id.message);
      message.setText("Loading . . .");
      dialogContainer.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
          @Override
          public void onGlobalLayout() {
              int width = dialogContainer.getWidth();
              window.setLayout(width, WindowManager.LayoutParams.WRAP_CONTENT);
              dialogContainer.getViewTreeObserver().removeOnGlobalLayoutListener(this);
          }
      });
      

      【讨论】:

        【解决方案3】:

        你试过用这个吗

        mydialog.getWindow().setLayout(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);

        写完mydialog.show()?

        应该可以。

        【讨论】:

          【解决方案4】:

          尝试下面的代码来创建一个包含换行内容的对话框。 (在对话框布局 xml 中使用 layout_width= WRAP_PATENT 和 height 作为 WRAP_CONTENT)

           WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
          
          
          lp.copyFrom(dialog.getWindow().getAttributes());
                  lp.width = WindowManager.LayoutParams.MATCH_PARENT;
                  lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
                  dialog.show();
                  dialog.getWindow().setAttributes(lp);
          

          【讨论】:

            【解决方案5】:

            我知道这个问题很老,但如果有人不想添加 WindowManager.LayoutParams,那么只需将对话框的主题设置为 android.R.style.Theme_Holo_Light_Dialog_NoActionBar,这将使对话框将其宽度包装到其内容中。

            例子:

            AlertDialog alert = new AlertDialog.Builder(context, android.R.style.Theme_Holo_Light_Dialog_NoActionBar).create();

            确保您正在膨胀的布局的高度和宽度都设置为wrap_content

            【讨论】:

            • 很棒的解决方案 :) 它正在工作,但它说“Resource.Style.ThemeHoloLightDialogNoActionBar 已过时:'不推荐使用'”将来应该使用哪种样式
            • 这个主题被贬低了
            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2018-01-13
            • 2013-06-24
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多