custom height and width in custom inflate alertdia

2019-08-18 16:49发布

问题:

i'm trying to inflate a custom layout in an alertdialog. I get it, but width is expanded to fill_parent. I have tried in xml file to put wrap_content or custom size (example 200dp) and it's ever fill_parent.

I want to put my own height and width, like (200dp, 150dp), how can i put this?

I have read and tried some solutions in this web, but i can't solve my problem, can anyone help me? thanks in advance for it.

I give my java code here

`>View ff = getLayoutInflater().inflate(R.layout.customlayout, null);
        >>Button menu = (Button)ff.findViewById(R.id.menu);
        >>
menu.setOnClickListener(new View.OnClickListener() {                                    
            >>
public void onClick(View v) {

>// TODO Auto-generated method stub 

>>gotomenu();
            }
            }); 
        >
AlertDialog dialog = new AlertDialog.Builder(this)


>.setView(ff)

> .create();        

> dialog.show();`

回答1:

if you want to make height and width on your own way than make custom class extend dialog like below

public class AlertCustomDialog extends Dialog {

public AlertCustomDialog(final Context context,  Event e) {
          requestWindowFeature(Window.FEATURE_NO_TITLE);
          setContentView(/*your layout*/);
          setCancelable(true);
 }

}

and call

    new  AlertCustomDialog(context, e).show();

when you need

otherwise you can define Activity and put code in manifest like below:

  <activity android:name="/*your activity name*/"
   android:theme="@android:style/Theme.Dialog"
        android:configChanges="orientation|keyboardHidden" />


回答2:

You can create LinearLayout and than add Space or just View to adjust height what you need