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

时间:2014-01-04 12:57:09

标签: android android-alertdialog

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

enter image description here

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

2 个答案:

答案 0 :(得分:3)

这可能会帮助你...一些典型的黑客......

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

答案 1 :(得分:0)

尝试以下方式创建自定义提醒对话框,

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