无论我尝试什么,AlertDialog都不会包含内容

时间:2017-06-23 09:25:54

标签: android android-alertdialog android-dialog

这是我的对话框的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。这个问题的简单方法是什么?

8 个答案:

答案 0 :(得分:2)

试试这个我的朋友

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

答案 1 :(得分:0)

尝试使用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);
}

}

答案 2 :(得分:0)

尝试将按钮宽度设置为0dp。另外,尝试使用工具 - &gt; android - &gt;布局检查员对您的活动。它可以让您更好地了解发生的事情

答案 3 :(得分:0)

我认为您应该在xml中定义宽度major和minor:

  

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>  

希望这会有所帮助。 抱歉我的英文。

答案 4 :(得分:0)

您正在做的一切正确,只需将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();

它会起作用。

答案 5 :(得分:0)

Try this it will work fine as you asked..

// Change your xml file by fallowing code

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 
 xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="match_parent"
 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="match_parent"
  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="match_parent"
  android:layout_height="wrap_content"
  android:orientation="horizontal"
  android:layout_below="@id/officer">

  <Button
   android:id="@+id/yes"
   android:layout_width="0dp"
   android:layout_weight="1"
   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="0dp"
   android:layout_weight="1"
   android:layout_height="42dp"
   android:background="#0078FF"
   android:textSize="15dp"
   android:textColor="@android:color/white"
   android:textAllCaps="false"
   android:text="No"/>
  </LinearLayout>

</RelativeLayout>

答案 6 :(得分:0)

val dialog = AlertDialog.Builder(context)
    .setView(myView)
    .create()


dialog.setOnShowListener {
    dialog.window?.setLayout(
        myView.width,
        ViewGroup.LayoutParams.WRAP_CONTENT
    )
}

dialog.show()


答案 7 :(得分:0)

以上内容都没有为我包装内容。 这是对我有用的东西。

我在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);

希望它也对您有用。