【问题标题】:AlertDialog won't wrap content no matter what I try无论我尝试什么,AlertDialog 都不会包装内容
【发布时间】:2017-11-26 19:41:04
【问题描述】:

这是我的对话框的 xml 文件

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/main"
android:paddingTop="23dp"
android:background="@android:color/white">

<ImageView
    android:id="@+id/officer"
    android:layout_width="47dp"
    android:layout_height="47dp"
    android:layout_marginLeft="24dp"
    android:background="@drawable/officerman"/>
<TextView
    android:id="@+id/text"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Are you sure you want\nto sign out?"
    android:textSize="15.5dp"
    android:textColor="#1C86FF"
    android:layout_marginTop="4dp"
    android:lineSpacingExtra="2dp"
    android:layout_marginLeft="84dp"/>

<LinearLayout
    android:layout_marginTop="20dp"
    android:layout_width="264dp"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:weightSum="1"
    android:layout_below="@id/officer">

    <Button
        android:id="@+id/yes"
        android:layout_width="wrap_content"
        android:layout_weight="0.5"
        android:layout_height="42dp"
        android:background="#1C86FF"
        android:text="Yes"
        android:textAllCaps="false"
        android:textSize="15dp"
        android:textColor="@android:color/white"/>

    <Button
        android:id="@+id/no"
        android:layout_width="wrap_content"
        android:layout_weight="0.5"
        android:layout_height="42dp"
        android:background="#0078FF"
        android:textSize="15dp"
        android:textColor="@android:color/white"
        android:textAllCaps="false"
        android:text="No"/>

</LinearLayout>

</RelativeLayout>

这是我的 java 代码

    AlertDialog.Builder build = new AlertDialog.Builder(Login.this);
    View lview = getLayoutInflater().inflate(R.layout.dialog_leave,null);
    build.setView(lview);
    AlertDialog dialog = build.create();
    dialog.show();

无论我尝试什么,alertdialog 大小都不会包裹到我的内容中。就这样结束了。它会在右侧添加扩展空间以使其达到特定大小。

Click on this to see the image. It adds extended space to the right see to get it to the default alertdialog size

我尝试过以像素为单位使用getWindow.setLayout(width,height),但这太痛苦了,必须有更好的方法来做到这一点。

我也试过getWindow.setLayout(Relative.LayoutParams.WRAP_CONTENT, ...),但同样的事情仍然发生。

我也尝试过使用popupwindow。解决此问题的简单方法是什么?

【问题讨论】:

  • 如果您在值中使用文字整数,您是否尝试过将android:layout_width="264dp" 设置为 xml 中的父级
  • 检查我的朋友

标签: android android-alertdialog android-dialog


【解决方案1】:

我的朋友试试这个

 AlertDialog.Builder build = new AlertDialog.Builder(Login.this);
    LayoutInflater mlLayoutInflater=LayoutInflater.from(MainActivity.this);
    final  View dialView=mlLayoutInflater.inflate(R.layout.R.layout.dialog_leave,null);
    AlertDialog dialog_card = build.create();
    Window window = dialog_card.getWindow();
    window.setLayout(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.WRAP_CONTENT);
    window.setGravity(Gravity.CENTER);
    build.show();

【讨论】:

    【解决方案2】:

    以上都没有为我包装内容。 这对我有用。

    我在styles.xml中添加了这个样式

    <style name="WrapContentDialog" parent="Theme.AppCompat.Light.Dialog">
        <item name="windowMinWidthMajor">0%</item>
        <item name="windowMinWidthMinor">0%</item>
    </style>
    

    然后我将它设置为这样的对话框。

    AlertDialog.Builder builder = new AlertDialog.Builder(getContext(), R.style.WrapContentDialog);
    

    希望它也对你有用。

    【讨论】:

      【解决方案3】:

      我认为您应该在 xml 中定义主要和次要宽度:

      windowFixedWidthMajor :窗口沿屏幕主轴的固定宽度,即横向时。

      windowFixedWidthMinor :窗口沿屏幕短轴的固定宽度,即纵向时。

      例如在你的 style.xml 中:

      <?xml version="1.0" encoding="utf-8"?>
      <resources>
          <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
              // your relevant item
              <item name="alertDialogTheme">@style/DialogTheme</item>
          </style>
      
          <style name="DialogTheme" parent="Theme.AppCompat.Light.Dialog">
              <item name="windowFixedWidthMajor">90%</item> 
              <item name="windowFixedWidthMinor">90%</item>  // this should fix your issue
          </style> 
      
       // your other styles 
      </resources>  
      

      希望这会有所帮助。 对不起我的英语。

      【讨论】:

      • 我使用 ContextThemeWrapper 进行了尝试,但没有成功。我应用样式错误吗?我该怎么做
      • 您不需要任何代码行,只需像上面一样通过添加项目alertDialogTheme 并添加样式“DialogTheme”来编辑您的 style.xml。不要忘记删除您的“黑客空间”。
      【解决方案4】:

      你做的一切都是对的,只需将你的 xml 布局更改为:

      <?xml version="1.0" encoding="utf-8"?>
      <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:background="#33000000"
          android:gravity="center">
      
      <RelativeLayout
          android:id="@+id/main"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:background="@android:color/white"
          android:paddingTop="23dp">
      
          <ImageView
              android:id="@+id/officer"
              android:layout_width="47dp"
              android:layout_height="47dp"
              android:layout_marginLeft="24dp"
          android:background="@drawable/officerman"/>
          <TextView
              android:id="@+id/text"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_marginLeft="84dp"
              android:layout_marginTop="4dp"
              android:lineSpacingExtra="2dp"
              android:text="Are you sure you want\nto sign out?"
              android:textColor="#1C86FF"
              android:textSize="15.5dp" />
      
          <LinearLayout
              android:layout_width="264dp"
              android:layout_height="wrap_content"
              android:layout_below="@id/officer"
              android:layout_marginTop="20dp"
              android:orientation="horizontal"
              android:weightSum="1">
      
              <Button
                  android:id="@+id/yes"
                  android:layout_width="wrap_content"
                  android:layout_height="42dp"
                  android:layout_weight="0.5"
                  android:background="#1C86FF"
                  android:text="Yes"
                  android:textAllCaps="false"
                  android:textColor="@android:color/white"
                  android:textSize="15dp" />
      
              <Button
                  android:id="@+id/no"
                  android:layout_width="wrap_content"
                  android:layout_height="42dp"
                  android:layout_weight="0.5"
                  android:background="#0078FF"
                  android:text="No"
                  android:textAllCaps="false"
                  android:textColor="@android:color/white"
                  android:textSize="15dp" />
      
          </LinearLayout>
      
      </RelativeLayout>
      </LinearLayout>
      

      Java 代码:

      final Dialog dialog = new Dialog(Login.this, android.R.style.Theme_Translucent_NoTitleBar);
         //dialog.setCancelable(false);
         View view = LayoutInflater.from(Login.this).inflate(R.layout.dialog_leave, null);
         dialog.setContentView(view);
         dialog.show();
      

      它会起作用的。

      【讨论】:

      • 谢谢,它确实有效,但外观动画不太流畅。它只是弹出而不是稍微淡入。你知道如何让它出现一个类似于警报对话框的动画吗?
      • 您可以为对话框创建自定义动画。参考:stackoverflow.com/questions/4817014/animate-a-custom-dialog
      • 创建您的动画 xml 并设置为 dialog.getWindow().getAttributes().windowAnimations = R.style.PauseDialogAnimation;
      • 这似乎是一个非常笨拙的解决方法,我正在寻找一个更简单的解决方案。我觉得有一个。
      • 创建样式后,您可以在项目的任何地方使用它
      【解决方案5】:

      尝试使用 DialogFragment

      public class DialogChangePasswordFragment extends DialogFragment {
      private Listener dialogButtonClick;
      private EditText editOldPassword;
      private EditText editNewPassword;
      private EditText editConfirmPassword;
      
      public static DialogChangePasswordFragment newInstance() {
          DialogChangePasswordFragment fragment = new DialogChangePasswordFragment();
          return fragment;
      }
      
      @Override
      public void onAttach(Context context) {
          super.onAttach(context);
          try {
              dialogButtonClick = (Listener) context;
          } catch (Exception e) {
              dialogButtonClick = (Listener) getTargetFragment();
              e.printStackTrace();
          }
      
      }
      
      @Override
      public void onResume() {
          super.onResume();
          getDialog().getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
      }
      
      @Override
      public void onDetach() {
          super.onDetach();
          dialogButtonClick = null;
      }
      
      @NonNull
      @Override
      public Dialog onCreateDialog(Bundle savedInstanceState) {
          Dialog dialog = super.onCreateDialog(savedInstanceState);
          dialog.getWindow().requestFeature(Window.FEATURE_NO_TITLE);
          return dialog;
      }
      
      @Nullable
      @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
          return inflater.inflate(R.layout.dialog_password_confirm, container, false);
      }
      
      @Override
      public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
          super.onViewCreated(view, savedInstanceState);
          TextView dialogOk = (TextView) view.findViewById(R.id.dialogOk);
          TextView dialogCancel = (TextView) view.findViewById(R.id.dialogCancel);
          dialogOk.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View v) {
                  if (validateFields()) {
                      dialogButtonClick.onConfirmChangePassword(editOldPassword.getText().toString().trim(), editConfirmPassword.getText().toString().trim());
                      dismiss();
                  }
              }
          });
          setCancelable(false);
          dialogCancel.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View v) {
                  dismiss();
              }
          });
      }
      
      
      
      public interface Listener {
          void onConfirmChangePassword(String oldPassword, String newPassword);
      }
      

      }

      【讨论】:

        【解决方案6】:

        尝试将按钮的宽度设置为 0dp。另外,尝试在您的活动上使用工具 -> android -> 布局检查器。它可能会让您更好地了解正在发生的事情

        【讨论】:

        • 还是一样
        • 尝试在布局检查器中捕获屏幕预览
        【解决方案7】:
        val dialog = AlertDialog.Builder(context)
            .setView(myView)
            .create()
        
        
        dialog.setOnShowListener {
            dialog.window?.setLayout(
                myView.width,
                ViewGroup.LayoutParams.WRAP_CONTENT
            )
        }
        
        dialog.show()
        
        
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2017-10-27
          • 2017-01-12
          • 1970-01-01
          • 2012-10-22
          • 1970-01-01
          • 2015-04-19
          • 1970-01-01
          • 2020-08-11
          相关资源
          最近更新 更多