我可以创建和显示一个自定义警报对话框很好,但即使这样,我有android:layout_width/height=“fill_parent”在对话xml中,它只和内容一样大。

我想要的是填充整个屏幕的对话框,除了20像素的填充。 然后,作为对话框一部分的图像将自动使用fill_parent拉伸到完整的对话框大小。


当前回答

这里有一个简短的答案,对我来说是有效的(在API 8和API 19上测试)。

Dialog mDialog;
View   mDialogView;
...
// Get height
int height = mDialog.getWindow()
.getWindowManager().getDefaultDisplay()
.getHeight();

// Set your desired padding (here 90%)
int padding = height - (int)(height*0.9f);

// Apply it to the Dialog
mDialogView.setPadding(
// padding left
0,
// padding top (90%)
padding, 
// padding right
0, 
// padding bottom (90%)
padding);

其他回答

到目前为止我能想到的最简单的方法

如果你的对话框是由垂直线性布局组成的,只需添加一个“高度填充”虚拟视图,它将占据屏幕的整个高度。

例如:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:orientation="vertical"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:weightSum="1">

    <EditText
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:id="@+id/editSearch" />

    <ListView
       android:layout_width="match_parent"
       android:layout_height="match_parent"
       android:id="@+id/listView"/>


   <!-- this is a dummy view that will make sure the dialog is highest -->
   <View
       android:layout_width="match_parent"
       android:layout_height="match_parent"
       android:layout_weight="1"/>

</LinearLayout>

注意LinearLayout属性中的android:weightSum="1"和dummy View属性中的android:layout_weight="1"

我发现了一个非常简单易行的解决办法

    fun showDialog(){
    val dialog = Dialog(this@DialogActivity)
    dialog.requestWindowFeature(Window.FEATURE_NO_TITLE)
    dialog.setCancelable(false)
    dialog.setContentView(R.layout.custom_dialog)
    val txtTitle = dialog.findViewById<TextView>(R.id.txtTitle)
    val btn  = dialog.findViewById<Button>(R.id.button)

    btn.setOnClickListener {
        Toast.makeText(this,"test",Toast.LENGTH_SHORT).show()
    }
    txtTitle.setText("ali")
    dialog.show()

    val window = dialog.window
    window?.setLayout(WindowManager.LayoutParams.MATCH_PARENT,WindowManager.LayoutParams.WRAP_CONTENT)
}

尝试将你的自定义对话框布局包装成RelativeLayout而不是LinearLayout。这对我很管用。

部分基于阿南德的回答。这对我来说很管用:

override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
    val fragmentActivity = requireActivity()
    val v = View.inflate(context, R.layout.fragment_about_dialog, null)
    val dialog = Dialog(fragmentActivity)
    dialog.requestWindowFeature(Window.FEATURE_NO_TITLE)
    dialog.setContentView(v)

    val wm = fragmentActivity.getSystemService(Context.WINDOW_SERVICE) as WindowManager 

    val display = if (VERSION.SDK_INT >= VERSION_CODES.R) {
        fragmentActivity.display
    } else {
        wm.defaultDisplay // deprecated in API 30
    }

    val size = Point()
    display?.getSize(size)

    val width = size.x - 50
    val height = size.y - 50
    val lp = WindowManager.LayoutParams()
    lp.copyFrom(dialog.window?.attributes)
    lp.width = width
    lp.height = height
    dialog.show()
    dialog.window?.attributes = lp
    
    return dialog
}

对于对话框布局使用constraintLayout:

<androidx.constraintlayout.widget.ConstraintLayout 
        android:id="@+id/dialogLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    ...
</androidx.constraintlayout.widget.ConstraintLayout>

结果:

这在改变屏幕方向时工作得很好。

    final AlertDialog alertDialog;

    LayoutInflater li = LayoutInflater.from(mActivity);
    final View promptsView = li.inflate(R.layout.layout_dialog_select_time, null);

    RecyclerView recyclerViewTime;
    RippleButton buttonDone;

    AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(mActivity);
    alertDialogBuilder.setView(promptsView);

    // create alert dialog
    alertDialog = alertDialogBuilder.create();

    /**
     * setting up window design
     */
    alertDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);


    alertDialog.show();

    DisplayMetrics metrics = new DisplayMetrics(); //get metrics of screen
    mActivity.getWindowManager().getDefaultDisplay().getMetrics(metrics);
    int height = (int) (metrics.heightPixels * 0.9); //set height to 90% of total
    int width = (int) (metrics.widthPixels * 0.9); //set width to 90% of total

    alertDialog.getWindow().setLayout(width, height); //set layout
    recyclerViewTime = promptsView.findViewById(R.id.recyclerViewTime);


    DialogSelectTimeAdapter dialogSelectTimeAdapter = new DialogSelectTimeAdapter(this);
    RecyclerView.LayoutManager linearLayoutManager = new LinearLayoutManager(this);
    recyclerViewTime.setLayoutManager(linearLayoutManager);
    recyclerViewTime.setAdapter(dialogSelectTimeAdapter);

    buttonDone = promptsView.findViewById(R.id.buttonDone);
    buttonDone.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            alertDialog.dismiss();

        }
    });