25

我正在尝试制作一个带有圆角的自定义 android 对话框。我目前的尝试给了我这个结果。

圆角对话框

如您所见,边角是圆形的,但白色边角仍然完好无损。

下面是我放入可绘制文件夹中的 xml,用于创建带有圆角的红色边框的蓝色对话框。

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item> 
        <shape 
            android:shape="rectangle">
            <solid android:color="@color/transparent_black" />
            <corners android:radius="@dimen/border_radius"/>
        </shape>
    </item>   
    <item 
        android:left="@dimen/border_width" 
        android:right="@dimen/border_width"  
        android:top="@dimen/border_width"
        android:bottom="@dimen/border_width" >  

        <shape android:shape="rectangle"> 
            <solid android:color="@color/blue" />
            <corners android:radius="@dimen/border_radius"/>
        </shape>
    </item>    
</layer-list>

下面是对话框的布局。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
style="@style/fill"
android:orientation="vertical"
android:layout_margin="@dimen/spacing_normal"
android:padding="@dimen/spacing_normal"

android:background="@drawable/border_error_dialog" >

<RelativeLayout 
    style="@style/block"
    android:layout_gravity="center" >

    <ImageView
        android:id="@+id/imageView1"
        style="@style/wrap"
        android:layout_alignParentLeft="true"
        android:layout_centerHorizontal="true"
        android:contentDescription="@string/content_description_filler"
        android:src="@drawable/ic_launcher" />

    <TextView
        android:id="@+id/textView1"
        style="@style/error_text"
        android:layout_centerVertical="true"
        android:layout_toRightOf="@+id/imageView1"
        android:text="@string/error_login" />

</RelativeLayout>

<Button
    android:id="@+id/button1"
    style="@style/wrap"
    android:layout_gravity="center"
    android:text="Button" />

</LinearLayout>

下面是我在其中创建对话框的活动。

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Button b1 = (Button) findViewById(R.id.button1);
    b1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {               
            AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(MainActivity.this);

            View child = getLayoutInflater().inflate(R.layout.dialog_custom_tom, null);
            alertDialogBuilder.setView(child);

            AlertDialog alertDialog = alertDialogBuilder.create();

            alertDialog.show();
        }
    });
}
4

11 回答 11

34

我找到的唯一解决方案是here。使用 Dialog 而不是 AlertDialog 并设置透明背景:
dialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
因此您不能使用构建器。但是,如果您遵循最佳指南,您也可以在 DialogFragment 的 onCreateDialog 回调中使用 new Dialog()。

这也适用于姜饼。

此外,可以将分层可绘制对象简化为一种形状,使用 xml 元素 <stroke> 作为边框。

于 2014-05-03T23:17:20.343 回答
19

在制作扩展 DialogFragment 的对话框并修复此问题时,我遇到了类似的问题:

dialog.setStyle(DialogFragment.STYLE_NO_FRAME, 0);

像这样:

public class ConfirmBDialog extends DialogFragment {

public static ConfirmBDialog newInstance() {
    ConfirmBDialog dialog = new ConfirmBDialog();
    Bundle bundle = new Bundle();
    dialog.setArguments(bundle);

    return dialog;
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // This removes black background below corners.
    setStyle(DialogFragment.STYLE_NO_FRAME, 0);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    View view = inflater.inflate(R.layout.confirm_dialog, container, true);
    getDialog().setCanceledOnTouchOutside(true);
    return view;
}

希望这可以帮助。

于 2013-10-22T15:08:34.727 回答
12

试试看嘛

myDialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
于 2014-05-15T10:31:03.947 回答
3

下面的代码解决了这个问题

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

谢谢,纳根德拉

于 2014-12-02T12:40:49.857 回答
3

在您的 java 文件中保留以下代码并更改您的布局名称

View mView =LayoutInflater.from(mContext).inflate(R.layout.layout_pob,null); 
alertDialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
于 2016-11-11T12:11:39.657 回答
3

试试用这个,这对我有用

dialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
于 2019-04-17T06:59:29.257 回答
2

Use 9-patch PNG with transparency in those corners.

于 2013-05-31T16:04:23.647 回答
1
 public void initDialog() {
    exitDialog = new Dialog(this);
    exitDialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
    View view = View.inflate(this, R.layout.dialoglayout, null);
    exitDialog.setContentView(view);
    AdSize adSize = new AdSize(300, 250);

    dialogAdview = new AdView(this);
    dialogAdview.setAdUnitId(getResources().getString(R.string.banner_id));
    dialogAdview.setAdSize(adSize);
    RelativeLayout adLayout = (RelativeLayout) view.findViewById(R.id.adLayout);
    adLayout.addView(dialogAdview);
    AdRequest adRequest = new AdRequest.Builder()
            .build();
    dialogAdview.loadAd(adRequest);
    dialogAdview.setAdListener(new AdListener() {
        @Override
        public void onAdLoaded() {
            Log.d("Tag", "adLoaded");
            super.onAdLoaded();
        }


    });

    view.findViewById(R.id.yes_btn).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            exit = true;
            onBackPressed();
        }
    });

    view.findViewById(R.id.no_btn).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            exit = false;
            exitDialog.dismiss();
        }
    });

}

对话框布局.xml

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:ads="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/custom_dialog_round"
    android:orientation="vertical">


    <TextView
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginLeft="10dp"
        android:text="Do you want to exit?"
        android:textColor="#000"
        android:textSize="18dp" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/text"
        android:orientation="horizontal">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_weight="1"
            android:gravity="center"
            android:orientation="horizontal">

            <TextView
                android:id="@+id/yes_btn"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/background_draw"
                android:padding="8dp"
                android:text="Yes"
                android:textAlignment="center"
                android:textColor="#9fa8da"
                android:textSize="20dp" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_weight="1"
            android:gravity="center"
            android:orientation="horizontal">

            <TextView
                android:id="@+id/no_btn"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginRight="10dp"
                android:background="@drawable/background_draw"
                android:padding="8dp"
                android:text="No"
                android:textAlignment="center"
                android:textColor="#d50000"
                android:textSize="20dp" />
        </LinearLayout>


    </LinearLayout>

</LinearLayout>
`

custom_dialog_round.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <solid
        android:color="#fff"/>
    <corners
        android:radius="10dp" />
    <padding
        android:left="10dp"
        android:top="10dp"
        android:right="10dp"
        android:bottom="10dp" />
</shape>

参考http://techamongus.blogspot.com/2018/02/android-create-round-corner-dialog.html

于 2018-02-11T17:26:56.243 回答
1
  • 更新

我知道活动的背景是有道理的。因此,请使用@robert 对这些更改的回答。

DialogFragment布局中设置宽度和高度或添加最小尺寸:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="wrap_content" // Or match_parent, 300dp.
    android:layout_height="wrap_content"
    android:layout_marginLeft="50dp"
    android:layout_marginRight="50dp"
    android:background="@drawable/white_round_corner_background"
    android:gravity="center"
    android:minWidth="300dp"
    android:minHeight="200dp"
    android:orientation="vertical"
    android:padding="15dp"
    >
...

从所需活动的样式中删除<item name="android:background">@color/...</item>并在活动的布局中设置这些背景。

DialogFragment写:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // This removes black background below corners.
    setStyle(DialogFragment.STYLE_NO_FRAME, 0);
}
  • 旧变种

根据罗伯特的回答,你应该申请setStyle(STYLE_NO_FRAME, 0),但出现了新的问题。如果您DialogFragment自定义对话框中的范围太小,那么您应该按照本指南进行操作。

添加到styles.xml这 3 行对话框大小:

<style name="ErrorDialogTheme" parent="@android:style/Theme.Dialog">
    <item name="android:minWidth" type="dimen">300dp</item>
    <!-- This option makes dialog fullscreen and adds black background, so I commented it -->
    <!-- <item name="android:minHeight" type="dimen">200dp</item> -->
    <!-- This option doesn't work, so I commented it -->
    <!-- <item name="android:layout_width">match_parent</item> -->
</style>

DialogFragment添加样式的布局中:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    ...
    android:minWidth="300dp" // Optional, remove this line.
    android:minHeight="200dp" // Optional, remove this line.
    style="@style/ErrorDialogTheme"
    android:theme="@style/ErrorDialogTheme"
    >

在您DialogFragment编写的代码中:

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    // This removes black background. If not 0 as a parameter, black background will appear.
    setStyle(STYLE_NO_FRAME, 0)
}

// If you want a fullscreen dialog, use this, but it doesn't remove a black background.
override fun onStart() {
    super.onStart()
    dialog.window?.setLayout(WindowManager.LayoutParams.MATCH_PARENT,
        WindowManager.LayoutParams.MATCH_PARENT)
}

查看AndroidManifest.xml并找到所有可以显示这些对话框的活动,检查android:theme="..."主题并转到styles.xml。现在看看<item name="android:background">@color/...</item>这些主题的项目。应该有透明的颜色,否则这些项目可能不存在。如果你有这些背景项目,那么整个活动也会有这些背景和对话框!所以,如果你在DialogFragment它上面有一个相机活动,你会看到这个。

在此处输入图像描述

删除所需样式的背景项目。也可能在代码中设置了背景,请检查它。

Android 中具有透明背景的对话框和许多页面中,它被写入添加以下之一:

dialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(0));
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));

可能在onViewCreated()or中onCreateDialog(),但它对我没有帮助,因为背景Activity是在样式中设置的。

在运行 Android 5.0.1 的三星 Galaxy S4 上测试。

于 2018-10-08T10:49:36.637 回答
1

使用 CardView 并制作

app:cardCornerRadius="dp" 根据形状 xml。

于 2019-04-09T19:42:34.083 回答
0

我将在这里发布我的解决方案,因为它可能会有所帮助。对我有用的解决方案是在布局 xml 以及启动对话框的活动中设置可绘制资源,而无需从 AlertDialog 切换到 Dialog。

这意味着在我们为对话框创建设计的布局中,alert_dialog_design.xml我们将android:background使用我们自己定义的背景填充属性alert_dialog_shape.xml

android:background="@drawable/alert_dialog_shape"

但也在启动对话框的活动内部:

alert.getWindow().setBackgroundDrawableResource(R.drawable.alert_dialog_shape);

这样,您的自定义布局的父级(警报本身)将具有您想要的形状。使用这种方法,我实现了以下目标:

https://i.stack.imgur.com/drCw3.png

于 2020-06-08T22:20:32.423 回答