2012-08-08 18 views
7

Tengo una tarea para cambiar el ancho y alto de AlertDialog por xml, quiero que se convierta en estilo, para que pueda usarlo fácilmente. Y eso, también necesito cambiar el botón del estilo AlertDialog. ¿Puede decirme una forma de lograr el target.Thank que muy agradecidos. PS, me gustaría lograr mejor el objetivo por el cambio xml.Android: ¿Cómo puedo configurar el ancho y alto de AlertDialog, y el botón del estilo AlertDialog?

+0

Puede tener algo como esto http://stackoverflow.com/questions/1979369/ android-activity-as-a-dialog –

+0

@ userIsAMonkey, muchas gracias, he pensado en usar Activity como un cuadro de diálogo, pero para mi tarea utilizar el cuadro de diálogo. porque hay muchos diálogos esperando para mostrar. Queremos tener un estilo fácil de mostrar. Gracias, alse. – oldfox3721

Respuesta

18

Hay dos métodos 1) programáticamente 2) mediante el uso de diseño xml

1)=======> 

AlertDialog.Builder builder = new AlertDialog.Builder(this); 
builder.setView(layout); 
builder.setTitle("Title"); 
alertDialog = builder.create(); 
alertDialog.show(); 
alertDialog.getWindow().setLayout(600, 400); //Controlling width and height. 


         (or) 

alertDialog.show(); 
WindowManager.LayoutParams lp = new WindowManager.LayoutParams(); 

lp.copyFrom(alertDialog.getWindow().getAttributes()); 
lp.width = 150; 
lp.height = 500; 
lp.x=-170; 
lp.y=100; 
alertDialog.getWindow().setAttributes(lp); 

Si desea para mostrar el diseño que se mostrará como Alert dialog ver this

2)========> 

choose.xml

<TextView 
    android:id="@+id/img" 
    android:layout_width="wrap_content" 
    android:text="@string/choose" 
    android:textSize="25dp" 
    android:textColor="#fff" 
    android:layout_height="50dp"/> 

<TableLayout android:id="@+id/table" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:background="#fff" 
    android:orientation="vertical"> 

    <TableRow 
     android:id="@+id/tr1" 
     android:orientation="horizontal" 
     android:layout_margin="10dp"> 
     <ImageView 
      android:contentDescription="@string/phone" 
      android:src="@drawable/phone" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content"/> 
     <TextView 
      android:id="@+id/phnText" 
      android:layout_width="wrap_content" 
      android:text="@string/phone" 
      android:gravity="left|center_vertical" 
      android:layout_marginLeft="10dp" 
      android:textSize="25dp" 
      android:textColor="#000" 
      android:layout_height="50dp"/> 
    </TableRow> 
    <View 
      android:layout_width="fill_parent" 
      android:layout_height="1dip" 
      android:background="#FF000000" /> 

    <TableRow 
     android:id="@+id/tr2" 
     android:orientation="horizontal" 
     android:layout_margin="10dp"> 
     <ImageView 
      android:contentDescription="@string/sms" 
      android:src="@drawable/sms" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content"/> 
     <TextView 
      android:id="@+id/smsText" 
      android:layout_width="wrap_content" 
      android:text="@string/sms" 
      android:gravity="left|center_vertical" 
      android:layout_marginLeft="10dp" 
      android:textSize="25dp" 
      android:textColor="#000" 
      android:layout_height="50dp"/> 
    </TableRow> 

</TableLayout> 
</LinearLayout> 

pantalla esto como emergente, como a continuación como

private void showPopUp() 
{ 
    final AlertDialog.Builder helpBuilder = new AlertDialog.Builder(this); 
    helpBuilder.setTitle(""); 

    LayoutInflater inflater = getLayoutInflater(); 
    final View checkboxLayout = inflater.inflate(R.layout.choose, null); 
    helpBuilder.setView(checkboxLayout); 

    final AlertDialog helpDialog = helpBuilder.create(); 
    helpDialog.show(); 

    TableRow tablerowPhone = (TableRow)checkboxLayout.findViewById(R.id.tr1); 
    TableRow tablerowSms = (TableRow)checkboxLayout.findViewById(R.id.tr2); 

    tablerowPhone.setOnClickListener(new OnClickListener() { 

     public void onClick(View v) { 

      helpDialog.dismiss(); 

      Uri callUri = Uri.parse("tel:" + listview_array[4]); 
      Intent intent = new Intent(Intent.ACTION_CALL, callUri); 
      startActivity(intent); 
     } 
    }); 

    tablerowSms.setOnClickListener(new OnClickListener() { 

     public void onClick(View v) { 

      helpDialog.dismiss(); 

      Uri smsUri = Uri.parse("sms:" + listview_array[4]); 
      Intent intent = new Intent(Intent.ACTION_VIEW, smsUri); 
      startActivity(intent); 
     } 
    }); 
} 

llamada showPopUp() este método en el que desea. por lo que se puede establecer la altura y la anchura de la distribución en el archivo xml

+0

Muchas gracias. De esta manera puede hacerlo, pero quiero hacerlo por archivo xml. Porque quiero configurar el xml a mi aplicación. ¿Tienes una forma de hacerlo por archivo xml? Gracias una y otra vez. – oldfox3721

1

probar esto, esto funciona para mí,

AlertDialog.Builder builder = new AlertDialog.Builder(this); 
builder.setView(layout); 
builder.setTitle("Title"); 
alertDialog = builder.create(); 
alertDialog.getWindow().setLayout(600, 400); //Controlling width and height. 
alertDialog.show(); 
Cuestiones relacionadas