Android 如何调整警报对话框的宽度?

Android 如何调整警报对话框的宽度?,android,android-alertdialog,Android,Android Alertdialog,在我的应用程序中,我创建了一个带有圆角的警告对话框,问题是它会以下图所示的宽度填充屏幕。如何避免这种情况,请帮助我。我已经粘贴了编码 MainActivity.java public class MainActivity extends Activity implements OnClickListener{ private static final int ALERT_DIALOG = 1; Dialog dialog; Context context;

在我的应用程序中,我创建了一个带有圆角的警告对话框,问题是它会以下图所示的宽度填充屏幕。如何避免这种情况,请帮助我。我已经粘贴了编码

MainActivity.java

public class MainActivity extends Activity implements OnClickListener{

    private static final int ALERT_DIALOG = 1;

   Dialog   dialog;
   Context context;

    @Override
    public void onCreate( Bundle savedInstanceState )
    {
        super.onCreate( savedInstanceState );


        setContentView( R.layout.activity_main );


        Button b = (Button)findViewById(R.id.button1);
          b.setOnClickListener(new OnClickListener()
                {

                @Override
                public void onClick(View arg0) {
                    // TODO Auto-generated method stub
                      showDialog( ALERT_DIALOG );
                }
                }
            );
    }

    @TargetApi(Build.VERSION_CODES.HONEYCOMB)
    @Override
    protected Dialog onCreateDialog( int id ){



        if ( id == ALERT_DIALOG )
        {
            //ContextThemeWrapper ctw = new ContextThemeWrapper( this, R.style.MyTheme );
        AlertDialog.Builder builder= new AlertDialog.Builder( this, R.style.MyTheme );

            builder.setMessage( "Hello World" )

                .setTitle( "Alert Dialog" )
                .setIcon( android.R.drawable.ic_dialog_alert )
                .setCancelable( false )
                .setPositiveButton( "No", new DialogInterface.OnClickListener()
                    {
                        public void onClick( DialogInterface dialog, int which )
                           {
                               // dialog.dismiss();
                           }

        })

        .setNegativeButton( "Yes", new DialogInterface.OnClickListener()
        {
            public void onClick( DialogInterface dialog, int which )
               {
                    dialog.dismiss();
               }
            });



dialog = builder.create();
}
        if ( dialog1 == null )
        {
            dialog = super.onCreateDialog( id );
        }
        return dialog;
     }

尝试以下方法创建自定义警报对话框

        // custom dialog
        final Dialog dialog = new Dialog(context);
        dialog.setContentView(R.layout.custom);
        dialog.setTitle("Title...");

        // set the custom dialog components - text, image and button
        TextView text = (TextView) dialog.findViewById(R.id.text);
        text.setText("Android custom dialog example!");
        ImageView image = (ImageView) dialog.findViewById(R.id.image);
        image.setImageResource(R.drawable.ic_launcher);

        Button dialogButton = (Button) dialog.findViewById(R.id.dialogButtonOK);
        // if button is clicked, close the custom dialog
        dialogButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });

        dialog.show();
custom.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <ImageView
        android:id="@+id/image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginRight="5dp" />

    <TextView
        android:id="@+id/text"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textColor="#FFF" 
        android:layout_toRightOf="@+id/image"/>/>

     <Button
        android:id="@+id/dialogButtonOK"
        android:layout_width="100px"
        android:layout_height="wrap_content"
        android:text=" Ok "
        android:layout_marginTop="5dp"
        android:layout_marginRight="5dp"
        android:layout_below="@+id/image"
        />

</RelativeLayout>

您可以使用填充自定义此.xml,这可能会帮助您。。。有些是多么典型的黑客行为

    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    AlertDialog alertDialog = builder.setPositiveButton("Yes", null)
            .setNegativeButton("No", null).setMessage("Message")
            .setTitle("Title").create();
    DisplayMetrics metrics = alertDialog.getContext().getResources()
            .getDisplayMetrics();
    final int width = metrics.widthPixels;
    alertDialog.setOnShowListener(new DialogInterface.OnShowListener() {

        @Override
        public void onShow(DialogInterface dialog) {
            AlertDialog alertDialog = (AlertDialog) dialog;
            View view = alertDialog.getWindow().getDecorView()
                    .findViewById(android.R.id.content);
            FrameLayout.LayoutParams layoutParams = (FrameLayout.LayoutParams) view.getLayoutParams();
            layoutParams.width = 4 * width / 5; // 80% of screen
            layoutParams.gravity = Gravity.CENTER;
            view.setLayoutParams(layoutParams);
            alertDialog.getWindow().setBackgroundDrawable(
                    new ColorDrawable(Color.TRANSPARENT));
        }
    });
    alertDialog.show();

尝试在.xml中添加一些填充的自定义AlertDialog。你在使用自定义主题吗???@june,我想你忘了编写dialog.show;在你的密码里。@june没有人。。。当我改变方向时它不起作用。。。你可以不接受答案,等待更好的答案。。。对不起。。。