Android AlertDialog问题

时间:2012-04-05 06:43:30

标签: android android-alertdialog

我有一个正常工作的AlertDialog。我已经使用以下代码为其设置了一些背景图像:

Button buttonPositive = (Button)dialog.getButton(DialogInterface.BUTTON_POSITIVE);
    Button buttonNegative = (Button)dialog.getButton(DialogInterface.BUTTON_NEGATIVE);
    buttonPositive.setBackgroundResource(R.drawable.custom_button);
    buttonPositive.setTextColor(Color.WHITE);
    buttonNegative.setBackgroundResource(R.drawable.custom_button);
    buttonNegative.setTextColor(Color.WHITE);

现在在设置图像之后,两个按钮相互接触,我的意思是它们之间没有空间。我尝试过使用setPadding(...),它不能正常工作。即使我正在改变图像尺寸(即宽度) )它不工作。任何帮助!!!

3 个答案:

答案 0 :(得分:1)

我认为最好创建你想要的布局xml文件......

并设置为alertDialog.setContentview(R.layout.mylayout);

答案 1 :(得分:0)

试试这段代码

private Dialog myDialog;

myDialog = new Dialog(ShowReportActivity.this);
myDialog.setContentView(R.layout.alert);// your xml 
myDialog.setTitle("Send Email");
myDialog.setCancelable(true);

Button set = (Button) myDialog  .findViewById(R.id.alert_bnt_send_email);
Button exit = (Button) myDialog.findViewById(R.id.alert_bnt_exit);

  set.setTextColor(Color.WHITE);
  set.setBackgroundResource(R.drawable.custom_button);

getMailId = (EditText) myDialog.findViewById(R.id.alert_editT_email_Id);
send.setOnClickListener(new OnClickListener() {


public void onClick(View v) {
                    .........
                      myDialog.dismiss();
        });


exit.setOnClickListener(new OnClickListener() {

    public void onClick(View v) {
        myDialog.dismiss();
            }
});

myDialog.show();

答案 2 :(得分:0)

使用如下所示的代码使用自定义对话框

 Dialog windialog = new Dialog(YourActivity.this);
 windialog.setContentView(R.layout.win_dialog);
 windialog.setTitle("Congratulation");
 windialog.setCancelable(true);
 final EditText et_emailverification=EditText)windialog.findViewById(R.id.et_emailveri);
 et_emailverification.setText(UserEmailOrName);
 Button submit=(Button)windialog.findViewById(R.id.bt_sub_que);
 submit.setOnClickListener(new     OnClickListener(){                                       

@Override
  public void onClick(View v) { 

       //write here your code what you want onclick
       }

 });
  Button cancel=(Button)windialog.findViewById(R.id.bt_sq_cancel);
  cancel.setOnClickListener(new OnClickListener(){
@Override
  public void onClick(View v) {
   // TODO Auto-generated method stub

 windialog.cancel();

 });
  windialog.show();

和xml一样win dialog.xml

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

    <LinearLayout
        android:id="@+id/linearLayout2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" 
        android:layout_marginRight="10dp"
       android:layout_marginLeft="10dp">

        <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Email "
        android:textAppearance="?android:attr/textAppearanceLarge" />

        <EditText
            android:id="@+id/et_emailveri"
            android:layout_width="250dp"
            android:layout_height="wrap_content"
            android:layout_weight="1" >


        </EditText>

    </LinearLayout>


    <RelativeLayout
        android:id="@+id/linearLayout2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:gravity="center"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/bt_sub_que"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Submit" />

         <Button
            android:id="@+id/bt_sq_cancel"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="0.50"
            android:text="Cancel" /> 

    </RelativeLayout>

   </LinearLayout>