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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24264630/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 07:57:45  来源:igfitidea点击:

Set custom dialog height and width

androidxmldialog

提问by Vineet Kumar

I am making a custom dialog of which I want height and width to be default fit to all screen sizes.

我正在制作一个自定义对话框,我希望其高度和宽度默认适合所有屏幕尺寸。

But it is not happening so. The dialog is appearing very small though. THis is the XML for the 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_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>

this is my dialog class

这是我的对话课

public class CustomDialogClass extends Dialog implements
            android.view.View.OnClickListener {

        public Activity c;
        public Dialog d;
        public Button yes, no;
        public TextView content;

        public CustomDialogClass(Activity a) {
            super(a);
            this.c = a;
        }

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            requestWindowFeature(Window.FEATURE_NO_TITLE);
            setContentView(R.layout.custom_dialog);
            yes = (Button) findViewById(R.id.yesButton);
            no = (Button) findViewById(R.id.noButton);
            content=(TextView)findViewById(R.id.txt_dia);
            yes.setOnClickListener(this);
            no.setOnClickListener(this);

        }

        @Override
        public void onClick(View v) {
            switch (v.getId()) {
            case R.id.yesButton:

                break;
            case R.id.noButton:
                dismiss();
                break;
            default:
                break;
            }
            dismiss();
        }
      }

How do I set the height and width to appear a default size such that it appears perfect on all screen size.

如何设置高度和宽度以显示默认尺寸,使其在所有屏幕尺寸上都显示完美。

I am a getting a very small dialog.

我是一个非常小的对话。

采纳答案by Jogendra Gouda

I have changed your lay put try now

我已经改变了你的躺着试试吧

 <?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>

回答by Rutvij

Not Dialog Class use, Try This Class :

不是对话框类使用,试试这个类:

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

回答by CodeToLife

In class extending Dialog :

在类扩展 Dialog :

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

enter image description here

enter image description here

Correctly for all screens.

适用于所有屏幕。

回答by Abdul Wasae

Put this.getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT); in your custom dialog's onCreate()

放入this.getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT); 您的自定义对话框onCreate()

回答by Tash Pemhiwa

You could add an android:gravity="center"to your LinearLayout and then set a minimum height on the LinearLayout as well, e.g. android:minHeight="120dp". You can then test your layout on various devices and tweak it until you get something that works on all screen sizes:

您可以android:gravity="center"向 LinearLayout添加,然后也可以在 LinearLayout 上设置最小高度,例如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" >