Android:AlertDialog具有自定义视图和圆角

Android:AlertDialog具有自定义视图和圆角,android,android-alertdialog,Android,Android Alertdialog,我正在尝试构建一个带有自定义视图(无标题或页脚)和圆角的AlertDialog。我看过很多关于如何做到这一点的帖子,我尝试了很多东西,但我不能像我想的那样构建它 这是我的目标: 我为名为dialog_background.xml的对话框创建了一个可绘制的: <shape xmlns:android="http://schemas.android.com/apk/res/android" > <solid android:color="#FFAAAAA

我正在尝试构建一个带有自定义
视图(无标题或页脚)和圆角的
AlertDialog
。我看过很多关于如何做到这一点的帖子,我尝试了很多东西,但我不能像我想的那样构建它

这是我的目标:

我为名为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>
我的自定义
视图的
布局将有两个按钮。现在,我向您展示一个空的
LinearLayout
,使其变得简单。这是playdialog.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    style="@style/MyDialog"
     >

</LinearLayout>
好的,如果我使用这样的代码,我会得到:

我试图将
对话框
背景设置为透明,修改我的
对话框片段
代码:

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; 
}
结果完全相同,因此我意识到我的
对话框下的白色矩形来自我的自定义
视图,而不是
对话框。我已经将我的
视图的背景设置为dialog_background.xml,因此我不能将其设置为透明,否则我会丢失角、颜色等

然后我决定使用dialog_background.xml修改
对话框的背景,并使我的视图以纯色作为背景

在我的自定义
视图
布局
(playdalog.xml)中,我将style=“@style/MyDialog”替换为:

然后在我的
对话框fragment
中,我使用了以下代码:

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; 
}
这就是我得到的:

这几乎是我想要的,但是你可以看到我的自定义
视图
边框,所以这还不够好。在这一点上,我不知道我还能做什么

有人知道我该怎么解决吗


谢谢

为什么不使用显示的图像,如“这是我的目标:”将目标bg图像保存在drawable文件夹中

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


我刚刚创建了一个自定义警报对话框。但它的棱角不是圆的

首先为它创建一个布局,如下所示-

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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_parent"
    android:layout_height="wrap_content"
    android:layout_margin="1sp"
    tools:ignore="UselessParent" >

    <TableLayout 
        android:id="@+id/tablelayout_dialog_title"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:stretchColumns="1" >

        <TableRow
            android:id="@+id/tablerow_dialog_title"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"            
            tools:ignore="UselessParent" >

            <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_parent" 
                android:background="#643c3a"
                android:padding="10sp"
                android:gravity="center_vertical"
                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_parent"
        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_parent"
        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_parent"
            android:layout_height="wrap_content"            
            tools:ignore="UselessParent" >

            <Button
                android:id="@+id/button_dialog_yes"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_margin="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_parent"
                android:layout_height="wrap_content"
                android:layout_margin="8sp"
                android:paddingTop="5sp"
                android:paddingBottom="5sp"
                android:background="@drawable/roundedcornerbuttonfordialog_shape"
                android:text="@string/string_no" />

        </TableRow>
    </TableLayout>

</RelativeLayout>
并将此方法称为-

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

希望这将对您有所帮助。

在我的情况下,要在对话框中删除边框,此解决方案非常有用:

dialog.setStyle(DialogFragment.STYLE_NO_FRAME, 0);

如本文所述,下面的代码解决了该问题

MyDialog mydialog = new MyDialog(this, "for testing",
            new myOnClickListener() {

                @Override
                public void onPositiveButtonClick() {
                    // TODO Auto-generated method stub
                    Toast.makeText(getApplicationContext(),
                            "I am positive button in the dialog",
                            Toast.LENGTH_LONG).show();
                }

                @Override
                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 around the Dialog
    mydialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
    mydialog.show();
谢谢,
Nagendra

像您一样,使用对话框而不是AlertDialog解决了问题。谢谢DIMO,我不会使用TableLayout来尝试进行格式化。我会坚持线性、相对和框架。扁平化视图层次结构可提高性能。您使用此
dialog.getWindow().setBackgroundDrawable(新的ColorDrawable(Color.WHITE)),节省了我的时间谢谢!它没有消除记录仪中的白色部分:(你能解决问题吗?对于圆角,我无法消除记录仪中的白色部分。出于可扩展性、计算重新渲染时间和不必要的功耗等原因,不建议使用图像库。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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_parent"
    android:layout_height="wrap_content"
    android:layout_margin="1sp"
    tools:ignore="UselessParent" >

    <TableLayout 
        android:id="@+id/tablelayout_dialog_title"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:stretchColumns="1" >

        <TableRow
            android:id="@+id/tablerow_dialog_title"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"            
            tools:ignore="UselessParent" >

            <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_parent" 
                android:background="#643c3a"
                android:padding="10sp"
                android:gravity="center_vertical"
                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_parent"
        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_parent"
        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_parent"
            android:layout_height="wrap_content"            
            tools:ignore="UselessParent" >

            <Button
                android:id="@+id/button_dialog_yes"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_margin="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_parent"
                android:layout_height="wrap_content"
                android:layout_margin="8sp"
                android:paddingTop="5sp"
                android:paddingBottom="5sp"
                android:background="@drawable/roundedcornerbuttonfordialog_shape"
                android:text="@string/string_no" />

        </TableRow>
    </TableLayout>

</RelativeLayout>
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();
}
String message = "Your Message";
callAlert(message, callingClass.this);
dialog.setStyle(DialogFragment.STYLE_NO_FRAME, 0);
MyDialog mydialog = new MyDialog(this, "for testing",
            new myOnClickListener() {

                @Override
                public void onPositiveButtonClick() {
                    // TODO Auto-generated method stub
                    Toast.makeText(getApplicationContext(),
                            "I am positive button in the dialog",
                            Toast.LENGTH_LONG).show();
                }

                @Override
                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 around the Dialog
    mydialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
    mydialog.show();