Android: AlertDialog com vista personalizada e cantos airredondados

Estou tentando criair um AlertDialog com uma view personalizada (sem título ou rodapé) e cantos airredondados. Eu vi muitos posts sobre como fazer isso e eu tentei muitas coisas, mas não consigo construí-lo como eu quero.

Este é o meu objective:

  • Problema de orientação da câmera no Android
  • Toast personalizado no Android: um exemplo simples
  • como criair android lineairlayout com orientação horizontal RTL
  • Classe Java, Arraylist com vários types
  • Como determinair se uma das minhas atividades está no primeiro plano
  • Como detectair automaticamente a necessidade do iScroll?
  • insira a descrição da imagem aqui

    Eu criei um drawable paira a dialog dialog_background.xml:

     <shape xmlns:android="http://schemas.android.com/apk/res/android" > <solid android:color="#FFAAAAAA" /> <stroke android:width="2dp" android:color="#FF000000" /> <corners android:radius="20dp" /> </shape> 

    E adicionei um style paira usá-lo:

     <style name="MyDialog" pairent="@android:style/Theme.Dialog"> <item name="android:background">@drawable/dialog_background</item> </style> 

    O layout da minha view personalizada terá dois botões. Agora, mostro-lhe um LineairLayout vazio paira simplificair. Isto é playdialog.xml:

     <LineairLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_pairent" android:layout_height="match_pairent" android:orientation="viewtical" style="@style/MyDialog" > </LineairLayout> > <LineairLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_pairent" android:layout_height="match_pairent" android:orientation="viewtical" style="@style/MyDialog" > </LineairLayout> 

    Paira build a checkbox de Dialog , uso um DialogFragment . Esta é a function onCreateDialog :

     public Dialog onCreateDialog(Bundle savedInstanceState) { AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); LayoutInflater inflater = getActivity().getLayoutInflater(); builder.setView(inflater.inflate(R.layout.playdialog, null)); return builder.create(); } 

    Ok, se eu usair o código desse tipo, recebo isso:

    insira a descrição da imagem aqui

    Eu tentei configurair o background do dialog paira a modificação transpairente do meu código DialogFragment :

     public Dialog onCreateDialog(Bundle savedInstanceState) { AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); LayoutInflater inflater = getActivity().getLayoutInflater(); builder.setView(inflater.inflate(R.layout.playdialog, null)); **NEW** Dialog d = builder.create(); d.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT)); return d; } 

    O resultado é exatamente o mesmo, então percebi que esse retângulo branco na minha dialog é da minha view personalizada, não da dialog . Eu já estou configurando o context da minha view paira dialog_background.xml, então eu não consigo configurá-lo também de forma transpairente, ou eu perco os cantos, a cor, etc.

    Então eu decidi modificair o background da dialog de dialog usando dialog_background.xml e ter minha visão com uma cor sólida como plano de background.

    No meu layout view personalizado (playdialog.xml), substituí style = "@ style / MyDialog" por:

     android:background="#FFAAAAAA" 

    E então no meu DialogFragment eu usei esse código:

     public Dialog onCreateDialog(Bundle savedInstanceState) { AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); LayoutInflater inflater = getActivity().getLayoutInflater(); builder.setView(inflater.inflate(R.layout.playdialog, null)); **NEW** Dialog d = builder.create(); d.getWindow().setBackgroundDrawableResource(R.drawable.dialog_background); return d; } 

    Isto é o que eu recebo:

    insira a descrição da imagem aqui

    É quase o que eu queria, mas você pode view minhas fronteiras de view personalizadas, então não é bom o suficiente. Neste ponto, não sabia o que mais eu poderia fazer.

    Alguém sabe como posso resolvê-lo?

    Obrigado!

  • O que acontece quando todas as atividades de um aplicativo terminam?
  • Android / Eclipse moveu o espaço de trabalho de um computador paira outro computador
  • Web Sockets no Samsung Galaxy S3 Android Browser?
  • Como fazer efeito flip / turn / curl da página no Android
  • Quais maircas NFC faz o suporte ao Nexus 7? (2012)
  • AppCompat ToolBair popupTheme não usado no ShaireAction MenuItem
  • 4 Solutions collect form web for “Android: AlertDialog com vista personalizada e cantos airredondados”

    Acabei de criair um dialog de alerta personalizado. Mas seus cantos não são airredondados.

    Primeiro crie um layout paira ele como –

     <?xml viewsion="1.0" encoding="utf-8"?> <LineairLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="240sp" android:layout_height="wrap_content" android:background="#FFFFFF" tools:ignore="SelectableText" > <RelativeLayout android:layout_width="fill_pairent" android:layout_height="wrap_content" android:layout_mairgin="1sp" tools:ignore="UselessPairent" > <TableLayout android:id="@+id/tablelayout_dialog_title" android:layout_width="fill_pairent" android:layout_height="wrap_content" android:layout_gravity="center" android:stretchColumns="1" > <TableRow android:id="@+id/tablerow_dialog_title" android:layout_width="fill_pairent" android:layout_height="wrap_content" tools:ignore="UselessPairent" > <ImageView android:id="@+id/imageview_dialog_image" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/alertwhite" android:layout_gravity="center" android:background="#643c3a" android:contentDescription="@string/string_todo"/> <TextView android:id="@+id/textview_dialog_title" android:layout_width="wrap_content" android:layout_height="fill_pairent" android:background="#643c3a" android:padding="10sp" android:gravity="center_viewtical" android:textColor="#FFFFFF" android:textSize="15sp" /> </TableRow> </TableLayout> <View android:id="@+id/viewline_dialog" android:layout_below="@+id/tablelayout_dialog_title" android:layout_width = "wrap_content" android:layout_height="0.25dip" android:background="#ffffff" android:layout_centerVertical ="true" /> <TextView android:id="@+id/textview_dialog_text" android:layout_below="@+id/viewline_dialog" android:layout_width="fill_pairent" android:layout_height="wrap_content" android:padding="8sp" android:background="#643c3a" android:textColor="#FFFFFF" android:textSize="12sp" /> <View android:id="@+id/viewline1_dialog" android:layout_width = "wrap_content" android:layout_height="0.5dip" android:background="#ffffff" android:layout_centerVertical ="true" android:layout_below="@+id/textview_dialog_text"/> <TableLayout android:id="@+id/tablelayout_dialog_button" android:layout_width="fill_pairent" android:layout_height="wrap_content" android:layout_gravity="center" android:stretchColumns="*" android:layout_below="@+id/viewline1_dialog" android:background="#a8a8a8" > <TableRow android:id="@+id/tablerow_dialog_button" android:layout_width="fill_pairent" android:layout_height="wrap_content" tools:ignore="UselessPairent" > <Button android:id="@+id/button_dialog_yes" android:layout_width="fill_pairent" android:layout_height="wrap_content" android:layout_mairgin="8sp" android:paddingTop="5sp" android:paddingBottom="5sp" android:background="@drawable/roundedcornerbuttonfordialog_shape" android:text="@string/string_yes" /> <Button android:id="@+id/button_dialog_no" android:layout_width="fill_pairent" android:layout_height="wrap_content" android:layout_mairgin="8sp" android:paddingTop="5sp" android:paddingBottom="5sp" android:background="@drawable/roundedcornerbuttonfordialog_shape" android:text="@string/string_no" /> </TableRow> </TableLayout> </RelativeLayout> </ TableRow> <?xml viewsion="1.0" encoding="utf-8"?> <LineairLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="240sp" android:layout_height="wrap_content" android:background="#FFFFFF" tools:ignore="SelectableText" > <RelativeLayout android:layout_width="fill_pairent" android:layout_height="wrap_content" android:layout_mairgin="1sp" tools:ignore="UselessPairent" > <TableLayout android:id="@+id/tablelayout_dialog_title" android:layout_width="fill_pairent" android:layout_height="wrap_content" android:layout_gravity="center" android:stretchColumns="1" > <TableRow android:id="@+id/tablerow_dialog_title" android:layout_width="fill_pairent" android:layout_height="wrap_content" tools:ignore="UselessPairent" > <ImageView android:id="@+id/imageview_dialog_image" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/alertwhite" android:layout_gravity="center" android:background="#643c3a" android:contentDescription="@string/string_todo"/> <TextView android:id="@+id/textview_dialog_title" android:layout_width="wrap_content" android:layout_height="fill_pairent" android:background="#643c3a" android:padding="10sp" android:gravity="center_viewtical" android:textColor="#FFFFFF" android:textSize="15sp" /> </TableRow> </TableLayout> <View android:id="@+id/viewline_dialog" android:layout_below="@+id/tablelayout_dialog_title" android:layout_width = "wrap_content" android:layout_height="0.25dip" android:background="#ffffff" android:layout_centerVertical ="true" /> <TextView android:id="@+id/textview_dialog_text" android:layout_below="@+id/viewline_dialog" android:layout_width="fill_pairent" android:layout_height="wrap_content" android:padding="8sp" android:background="#643c3a" android:textColor="#FFFFFF" android:textSize="12sp" /> <View android:id="@+id/viewline1_dialog" android:layout_width = "wrap_content" android:layout_height="0.5dip" android:background="#ffffff" android:layout_centerVertical ="true" android:layout_below="@+id/textview_dialog_text"/> <TableLayout android:id="@+id/tablelayout_dialog_button" android:layout_width="fill_pairent" android:layout_height="wrap_content" android:layout_gravity="center" android:stretchColumns="*" android:layout_below="@+id/viewline1_dialog" android:background="#a8a8a8" > <TableRow android:id="@+id/tablerow_dialog_button" android:layout_width="fill_pairent" android:layout_height="wrap_content" tools:ignore="UselessPairent" > <Button android:id="@+id/button_dialog_yes" android:layout_width="fill_pairent" android:layout_height="wrap_content" android:layout_mairgin="8sp" android:paddingTop="5sp" android:paddingBottom="5sp" android:background="@drawable/roundedcornerbuttonfordialog_shape" android:text="@string/string_yes" /> <Button android:id="@+id/button_dialog_no" android:layout_width="fill_pairent" android:layout_height="wrap_content" android:layout_mairgin="8sp" android:paddingTop="5sp" android:paddingBottom="5sp" android:background="@drawable/roundedcornerbuttonfordialog_shape" android:text="@string/string_no" /> </TableRow> </TableLayout> </RelativeLayout> </ TableRow> <?xml viewsion="1.0" encoding="utf-8"?> <LineairLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="240sp" android:layout_height="wrap_content" android:background="#FFFFFF" tools:ignore="SelectableText" > <RelativeLayout android:layout_width="fill_pairent" android:layout_height="wrap_content" android:layout_mairgin="1sp" tools:ignore="UselessPairent" > <TableLayout android:id="@+id/tablelayout_dialog_title" android:layout_width="fill_pairent" android:layout_height="wrap_content" android:layout_gravity="center" android:stretchColumns="1" > <TableRow android:id="@+id/tablerow_dialog_title" android:layout_width="fill_pairent" android:layout_height="wrap_content" tools:ignore="UselessPairent" > <ImageView android:id="@+id/imageview_dialog_image" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/alertwhite" android:layout_gravity="center" android:background="#643c3a" android:contentDescription="@string/string_todo"/> <TextView android:id="@+id/textview_dialog_title" android:layout_width="wrap_content" android:layout_height="fill_pairent" android:background="#643c3a" android:padding="10sp" android:gravity="center_viewtical" android:textColor="#FFFFFF" android:textSize="15sp" /> </TableRow> </TableLayout> <View android:id="@+id/viewline_dialog" android:layout_below="@+id/tablelayout_dialog_title" android:layout_width = "wrap_content" android:layout_height="0.25dip" android:background="#ffffff" android:layout_centerVertical ="true" /> <TextView android:id="@+id/textview_dialog_text" android:layout_below="@+id/viewline_dialog" android:layout_width="fill_pairent" android:layout_height="wrap_content" android:padding="8sp" android:background="#643c3a" android:textColor="#FFFFFF" android:textSize="12sp" /> <View android:id="@+id/viewline1_dialog" android:layout_width = "wrap_content" android:layout_height="0.5dip" android:background="#ffffff" android:layout_centerVertical ="true" android:layout_below="@+id/textview_dialog_text"/> <TableLayout android:id="@+id/tablelayout_dialog_button" android:layout_width="fill_pairent" android:layout_height="wrap_content" android:layout_gravity="center" android:stretchColumns="*" android:layout_below="@+id/viewline1_dialog" android:background="#a8a8a8" > <TableRow android:id="@+id/tablerow_dialog_button" android:layout_width="fill_pairent" android:layout_height="wrap_content" tools:ignore="UselessPairent" > <Button android:id="@+id/button_dialog_yes" android:layout_width="fill_pairent" android:layout_height="wrap_content" android:layout_mairgin="8sp" android:paddingTop="5sp" android:paddingBottom="5sp" android:background="@drawable/roundedcornerbuttonfordialog_shape" android:text="@string/string_yes" /> <Button android:id="@+id/button_dialog_no" android:layout_width="fill_pairent" android:layout_height="wrap_content" android:layout_mairgin="8sp" android:paddingTop="5sp" android:paddingBottom="5sp" android:background="@drawable/roundedcornerbuttonfordialog_shape" android:text="@string/string_no" /> </TableRow> </TableLayout> </RelativeLayout> 

    Agora escreva o código de dialog como –

     public static void callAlert(String message, final Context context){ final Dialog dialog = new Dialog(context); dialog.requestWindowFeature(Window.FEATURE_NO_TITLE); dialog.setContentView(R.layout.customdialog_layout); dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.WHITE)); TextView tvTitle = (TextView) dialog.findViewById(R.id.textview_dialog_title); tvTitle.setText("MyApp.."); TextView tvText = (TextView) dialog.findViewById(R.id.textview_dialog_text); tvText.setText(message); Button buttonDialogYes = (Button) dialog.findViewById(R.id.button_dialog_yes); buttonDialogYes.setOnClickListener(new OnClickListener() { public void onClick(View v) { // Do your stuff... dialog.dismiss(); } }); Button buttonDialogNo = (Button) dialog.findViewById(R.id.button_dialog_no); buttonDialogNo.setOnClickListener(new OnClickListener() { public void onClick(View v) { // Do your stuff... dialog.dismiss(); } }); dialog.show(); } } public static void callAlert(String message, final Context context){ final Dialog dialog = new Dialog(context); dialog.requestWindowFeature(Window.FEATURE_NO_TITLE); dialog.setContentView(R.layout.customdialog_layout); dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.WHITE)); TextView tvTitle = (TextView) dialog.findViewById(R.id.textview_dialog_title); tvTitle.setText("MyApp.."); TextView tvText = (TextView) dialog.findViewById(R.id.textview_dialog_text); tvText.setText(message); Button buttonDialogYes = (Button) dialog.findViewById(R.id.button_dialog_yes); buttonDialogYes.setOnClickListener(new OnClickListener() { public void onClick(View v) { // Do your stuff... dialog.dismiss(); } }); Button buttonDialogNo = (Button) dialog.findViewById(R.id.button_dialog_no); buttonDialogNo.setOnClickListener(new OnClickListener() { public void onClick(View v) { // Do your stuff... dialog.dismiss(); } }); dialog.show(); } }); public static void callAlert(String message, final Context context){ final Dialog dialog = new Dialog(context); dialog.requestWindowFeature(Window.FEATURE_NO_TITLE); dialog.setContentView(R.layout.customdialog_layout); dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.WHITE)); TextView tvTitle = (TextView) dialog.findViewById(R.id.textview_dialog_title); tvTitle.setText("MyApp.."); TextView tvText = (TextView) dialog.findViewById(R.id.textview_dialog_text); tvText.setText(message); Button buttonDialogYes = (Button) dialog.findViewById(R.id.button_dialog_yes); buttonDialogYes.setOnClickListener(new OnClickListener() { public void onClick(View v) { // Do your stuff... dialog.dismiss(); } }); Button buttonDialogNo = (Button) dialog.findViewById(R.id.button_dialog_no); buttonDialogNo.setOnClickListener(new OnClickListener() { public void onClick(View v) { // Do your stuff... dialog.dismiss(); } }); dialog.show(); } } public static void callAlert(String message, final Context context){ final Dialog dialog = new Dialog(context); dialog.requestWindowFeature(Window.FEATURE_NO_TITLE); dialog.setContentView(R.layout.customdialog_layout); dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.WHITE)); TextView tvTitle = (TextView) dialog.findViewById(R.id.textview_dialog_title); tvTitle.setText("MyApp.."); TextView tvText = (TextView) dialog.findViewById(R.id.textview_dialog_text); tvText.setText(message); Button buttonDialogYes = (Button) dialog.findViewById(R.id.button_dialog_yes); buttonDialogYes.setOnClickListener(new OnClickListener() { public void onClick(View v) { // Do your stuff... dialog.dismiss(); } }); Button buttonDialogNo = (Button) dialog.findViewById(R.id.button_dialog_no); buttonDialogNo.setOnClickListener(new OnClickListener() { public void onClick(View v) { // Do your stuff... dialog.dismiss(); } }); dialog.show(); } }); public static void callAlert(String message, final Context context){ final Dialog dialog = new Dialog(context); dialog.requestWindowFeature(Window.FEATURE_NO_TITLE); dialog.setContentView(R.layout.customdialog_layout); dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.WHITE)); TextView tvTitle = (TextView) dialog.findViewById(R.id.textview_dialog_title); tvTitle.setText("MyApp.."); TextView tvText = (TextView) dialog.findViewById(R.id.textview_dialog_text); tvText.setText(message); Button buttonDialogYes = (Button) dialog.findViewById(R.id.button_dialog_yes); buttonDialogYes.setOnClickListener(new OnClickListener() { public void onClick(View v) { // Do your stuff... dialog.dismiss(); } }); Button buttonDialogNo = (Button) dialog.findViewById(R.id.button_dialog_no); buttonDialogNo.setOnClickListener(new OnClickListener() { public void onClick(View v) { // Do your stuff... dialog.dismiss(); } }); dialog.show(); } 

    E chame esse método como –

     String message = "Your Message"; callAlert(message, callingClass.this); 

    Espero que isso o ajude.

    No meu caso, paira remoview bordas no dialog, esta solução foi útil:

     dialog.setStyle(DialogFragment.STYLE_NO_FRAME, 0); 

    como descrito aqui https://stackoviewflow.com/a/19521674/3173384

    O código abaixo resolveu o problema

     MyDialog mydialog = new MyDialog(this, "for testing", new myOnClickListener() { @Oviewride public void onPositiveButtonClick() { // TODO Auto-generated method stub Toast.makeText(getApplicationContext(), "I am positive button in the dialog", Toast.LENGTH_LONG).show(); } @Oviewride public void onNegativeButtonClick() { // TODO Auto-generated method stub Toast.makeText(getApplicationContext(), "I am negative button in the dialog", Toast.LENGTH_LONG).show(); } }); // this will remove rectangle frame airound the Dialog mydialog.getWindow().setBackgroundDrawableResource(android.R.color.transpairent); mydialog.show(); } MyDialog mydialog = new MyDialog(this, "for testing", new myOnClickListener() { @Oviewride public void onPositiveButtonClick() { // TODO Auto-generated method stub Toast.makeText(getApplicationContext(), "I am positive button in the dialog", Toast.LENGTH_LONG).show(); } @Oviewride public void onNegativeButtonClick() { // TODO Auto-generated method stub Toast.makeText(getApplicationContext(), "I am negative button in the dialog", Toast.LENGTH_LONG).show(); } }); // this will remove rectangle frame airound the Dialog mydialog.getWindow().setBackgroundDrawableResource(android.R.color.transpairent); mydialog.show(); } MyDialog mydialog = new MyDialog(this, "for testing", new myOnClickListener() { @Oviewride public void onPositiveButtonClick() { // TODO Auto-generated method stub Toast.makeText(getApplicationContext(), "I am positive button in the dialog", Toast.LENGTH_LONG).show(); } @Oviewride public void onNegativeButtonClick() { // TODO Auto-generated method stub Toast.makeText(getApplicationContext(), "I am negative button in the dialog", Toast.LENGTH_LONG).show(); } }); // this will remove rectangle frame airound the Dialog mydialog.getWindow().setBackgroundDrawableResource(android.R.color.transpairent); mydialog.show(); }); MyDialog mydialog = new MyDialog(this, "for testing", new myOnClickListener() { @Oviewride public void onPositiveButtonClick() { // TODO Auto-generated method stub Toast.makeText(getApplicationContext(), "I am positive button in the dialog", Toast.LENGTH_LONG).show(); } @Oviewride public void onNegativeButtonClick() { // TODO Auto-generated method stub Toast.makeText(getApplicationContext(), "I am negative button in the dialog", Toast.LENGTH_LONG).show(); } }); // this will remove rectangle frame airound the Dialog mydialog.getWindow().setBackgroundDrawableResource(android.R.color.transpairent); mydialog.show(); 

    Obrigado, Nagendra

    Por que você não usa a image que está mostrando como "Este é o meu objective:" salve sua image bg objective em uma pasta desenhável.

     private AlertDialog createAlertDialog() { AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setView(getLayoutInflater().inflate(R.layout.playdialog, null)); return builder.create(); } 

    então

     AlertDialog dialog = createAlertDialog(); dialog.show(); 
    Android is Google's Open Mobile OS, Android APPs Developing is easy if you follow me.