Android 设置自定义对话框的高度和宽度

Android 设置自定义对话框的高度和宽度,android,xml,dialog,Android,Xml,Dialog,我正在制作一个自定义对话框,我希望它的高度和宽度默认适合所有屏幕大小 但事实并非如此。 不过对话框看起来很小。 这是对话框的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="wrap

我正在制作一个自定义对话框,我希望它的高度和宽度默认适合所有屏幕大小

但事实并非如此。 不过对话框看起来很小。 这是对话框的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="wrap_content"
android:background="#D24379"
android:orientation="vertical" >

<TextView
    android:id="@+id/txt_dia"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:layout_margin="10dp"
    android:textColor="@android:color/white"
    android:textSize="15dp"
    android:textStyle="bold" >
</TextView>

 <LinearLayout
    android:id="@+id/layButton"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="5dp"
    android:orientation="horizontal" >

    <Button
        android:id="@+id/yesButton"
        android:layout_width="0.0dip"
        android:layout_height="wrap_content"
        android:layout_weight="1.0"
        android:background="#ffffff"
        android:text="@string/yes" />

    <Button
        android:id="@+id/noButton"
        android:layout_marginLeft="5dp"
        android:layout_width="0.0dip"
        android:background="#ffffff"
        android:layout_height="wrap_content"
        android:layout_weight="1.0"
        android:text="@string/no" />
</LinearLayout>
如何设置高度和宽度以显示默认大小,从而使其在所有屏幕大小上都显示完美


我得到了一个非常小的对话框。

不使用对话框类,请尝试此类:

    public class MainActivity extends Activity {

        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);


                     ShowDialog();

        }

        private void ShowDialog() {
                // TODO Auto-generated method stub
                final Dialog dialog = new Dialog(context,
                        android.R.style.Theme_Translucent_NoTitleBar);
                dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
                dialog.setContentView(R.layout.custom_dialog);
                WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
                lp.copyFrom(dialog.getWindow().getAttributes());
                lp.width = WindowManager.LayoutParams.MATCH_PARENT;
                lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
                lp.gravity = Gravity.CENTER;

                dialog.getWindow().setAttributes(lp);

                Button yes = (Button) dialog.findViewById(R.id.yesButton);
                yes.setOnClickListener(new OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        // TODO Auto-generated method stub

                        dialog.dismiss();

                    }
                });

                Button no = (Button) dialog.findViewById(R.id.noButton);
                no.setOnClickListener(new OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        // TODO Auto-generated method stub
                        Intent i = new Intent(
                                Intent.ACTION_PICK,

                        dialog.dismiss();

                    }
                });

                  TextView content =(TextView) dialog.findViewById(R.id.txt_dia);



                dialog.show();
            }
}

您可以在线性布局中添加一个
android:gravity=“center”
,然后在线性布局上设置最小高度,例如
android:minHeight=“120dp”
。然后,您可以在各种设备上测试布局,并对其进行调整,直到获得适用于所有屏幕尺寸的布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="120dp"
android:gravity="center"
android:background="#D24379"
android:orientation="vertical" >

我已更改了您的放码方式,请立即尝试

 <?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="150dp"
        android:background="#D24379"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/txt_dia"
            android:layout_width="match_parent"
            android:layout_height="30dp"
            android:layout_marginTop="10dp"
            android:layout_weight="0.20"
            android:text="Are you good?"
            android:textColor="@android:color/white"
            android:textSize="15dp"
            android:textStyle="bold" />

        <LinearLayout
            android:id="@+id/layButton"
            android:layout_width="match_parent"
            android:layout_height="80dp"
            android:layout_margin="5dp"
            android:gravity="center"
            android:orientation="horizontal" >

            <Button
                android:id="@+id/yesButton"
                android:layout_width="0.0dip"
                android:layout_height="wrap_content"
                android:layout_weight="1.0"
                android:background="#ffffff"
                android:text="YES" />

            <Button
                android:id="@+id/noButton"
                android:layout_width="0.0dip"
                android:layout_height="wrap_content"
                android:layout_marginLeft="5dp"
                android:layout_weight="1.0"
                android:background="#ffffff"
                android:text="NO" />
        </LinearLayout>

    </LinearLayout>

在类扩展对话框中:

public MyPopUp(MainActivity activity, String sPrm) {
    super(activity);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.alert_dialog);
    LinearLayout bedLcl = (LinearLayout) findViewById(R.id.bed);
    TextView textLcl = (TextView) findViewById(R.id.dialog_text);
    textLcl.setText(sPrm);
    setOnCancelListener(this);
    DisplayMetrics displayMetrics = new DisplayMetrics();
 activity.getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
    int widthLcl = (int) (displayMetrics.widthPixels*0.9f);
    int heightLcl = (int) (displayMetrics.heightPixels*0.9f);
    FrameLayout.LayoutParams paramsLcl = (FrameLayout.LayoutParams) 
    bedLcl.getLayoutParams();
    paramsLcl.width = widthLcl;
    paramsLcl.height =heightLcl ;
    paramsLcl.gravity = Gravity.CENTER;
    show();
    Window window = getWindow();
    bedLcl .setLayoutParams(paramsLcl);
    window .setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));}

对所有屏幕都正确。

Put
this.getWindow().setLayout(ViewGroup.LayoutParams.MATCH_父级,ViewGroup.LayoutParams.WRAP_内容);

在自定义对话框的
onCreate()

dialog.getWindow().setAttributes(lp)中需要在
dialog.show()之后进行设置。
您应该详细说明更改的内容。
public MyPopUp(MainActivity activity, String sPrm) {
    super(activity);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.alert_dialog);
    LinearLayout bedLcl = (LinearLayout) findViewById(R.id.bed);
    TextView textLcl = (TextView) findViewById(R.id.dialog_text);
    textLcl.setText(sPrm);
    setOnCancelListener(this);
    DisplayMetrics displayMetrics = new DisplayMetrics();
 activity.getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
    int widthLcl = (int) (displayMetrics.widthPixels*0.9f);
    int heightLcl = (int) (displayMetrics.heightPixels*0.9f);
    FrameLayout.LayoutParams paramsLcl = (FrameLayout.LayoutParams) 
    bedLcl.getLayoutParams();
    paramsLcl.width = widthLcl;
    paramsLcl.height =heightLcl ;
    paramsLcl.gravity = Gravity.CENTER;
    show();
    Window window = getWindow();
    bedLcl .setLayoutParams(paramsLcl);
    window .setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));}