简体   繁体   English

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

[英]Android: AlertDialog with custom view and rounded corners

I'm trying to build an AlertDialog with a custom view (no title or footer) and rounded corners. 我正在尝试使用自定义view (无标题或页脚)和圆角构建AlertDialog I've seen a lot of posts about how to do that and I tried a lot of things, but I can't build it like I want to. 我已经看到了很多有关如何执行此操作的文章,并且尝试了很多事情,但是我无法按照自己的意愿来构建它。

This is my goal: 这是我的目标:

在此处输入图片说明

I created a drawable for the dialog called dialog_background.xml: 我为dialog创建了一个名为dialog_background.xml的drawable对象:

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

And I added a style to use it: 我添加了一种style来使用它:

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

The layout of my custom view is gonna have two buttons. 我的自定义viewlayout将有两个按钮。 Right now I show you an empty LinearLayout to make it simple. 现在,我向您展示了一个空的LinearLayout以使其变得简单。 This is playdialog.xml: 这是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>

To build the Dialog I use a DialogFragment . 要构建Dialog我使用DialogFragment This is its onCreateDialog 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, if I use the code like that I get this: 好吧,如果我使用类似的代码,我得到以下信息:

在此处输入图片说明

I tried to set the dialog background to transparent modifying my DialogFragment code: 我试图将dialog背景设置为透明,以修改我的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; 
}

The result is exactly the same, so then I realized that that white rectangle under my dialog is from my custom view , not from the dialog . 结果是完全相同的,因此我意识到dialog下面的白色矩形来自我的自定义view ,而不是来自dialog I'm already setting my view 's background to dialog_background.xml, so I can't set it to transparent too or I loose the corners, color, etc. 我已经将view的背景设置为dialog_background.xml,所以我也不能将其设置为透明,否则会丢失边角,颜色等。

Then I decided to modify the dialog 's background using dialog_background.xml and have my view have a solid color as background. 然后,我决定使用dialog_background.xml修改dialog的背景,并使我的视图具有纯色作为背景。

In my custom view layout (playdialog.xml) I replaced style="@style/MyDialog" with: 在我的自定义view layout (playdialog.xml)中,我将style =“ @ style / MyDialog”替换为:

android:background="#FFAAAAAA"

And then in my DialogFragment I used this code: 然后在我的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().setBackgroundDrawableResource(R.drawable.dialog_background);
    return d; 
}

This is what I get: 这是我得到的:

在此处输入图片说明

It's almost what I wanted, but you can see my custom view borders, so it's not good enough. 这几乎是我想要的,但是您可以看到我的自定义view边框,因此不够好。 At this point I didn't know what else could I do. 在这一点上,我不知道还能做什么。

Anyone knows how can I solve it? 谁知道我该怎么解决?

Thanks! 谢谢!

I just created a custom alert dialog. 我刚刚创建了一个自定义警报对话框。 But its corners are not rounded. 但是它的角落不是圆的。

First create a layout for it as - 首先为它创建一个布局-

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

Now write the code of dialog as - 现在将对话框代码编写为-

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

And call this method as - 并将此方法称为-

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

Hope this will help you. 希望这会帮助你。

In my case, to remove borders in dialog this solution was helpfull: 就我而言,要删除对话框中的边框,此解决方案很有帮助:

dialog.setStyle(DialogFragment.STYLE_NO_FRAME, 0);

like described here https://stackoverflow.com/a/19521674/3173384 就像这里描述的https://stackoverflow.com/a/19521674/3173384

The below code solved the issue 下面的代码解决了这个问题

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

Thanks, Nagendra 谢谢,Nagendra

why don't you use the image that you are showing like "This is my goal:" save your goal bg image in drawable folder. 为什么不使用显示的图像作为“这是我的目标:”将目标bg图像保存在drawable文件夹中。

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

then 然后

AlertDialog dialog = createAlertDialog();
dialog.show();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM