What I am trying to do:我正在try 用android制作一个圆角自定义对话框.

What is happening:我可以自定义对话框,但它没有圆角.我try 添加一个 Select 器,但仍然无法实现圆角.

下面是我的代码:


Java code:

private void launchDismissDlg() {

        dialog = new Dialog(getActivity(), android.R.style.Theme_Dialog);
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialog.setContentView(R.layout.dlg_dismiss);
        dialog.setCanceledOnTouchOutside(true);

        Button btnReopenId = (Button) dialog.findViewById(R.id.btnReopenId);
        Button btnCancelId = (Button) dialog.findViewById(R.id.btnCancelId);

        btnReopenId.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {



            }
        });


        btnCancelId.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {



            }
        });
        dialog.setCanceledOnTouchOutside(false);
        dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
        dialog.getWindow().setLayout(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT);
        dialog.show();

    }

xml code:

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

    <TableLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <TableRow
            android:id="@+id/tableRow1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="20dp"
            android:gravity="center" >

            <TextView
                android:id="@+id/textView1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:text="&quot;I WOULD LIKE TO DISMISS THE VENDOR&quot;"
                android:textColor="@color/col_dlg_blue_light"
                android:textSize="14sp"
                android:textStyle="bold" />
        </TableRow>

        <TableRow
            android:id="@+id/tableRow2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="5dp"
            android:gravity="center" >

            <TextView
                android:id="@+id/textView2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:text="BECAUSE"
                android:textColor="@android:color/black"
                android:textStyle="bold" />
        </TableRow>



        <TableRow
            android:id="@+id/tableRow4"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >

            <Button
                android:id="@+id/btnReopenId"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:background="@color/col_dlg_green_light"
                android:text="REOPEN"
                android:padding="5dp"
                android:textSize="14sp"
                android:textColor="@android:color/white"
                android:textStyle="bold" />

            <Button
                android:id="@+id/btnCancelId"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:background="@color/col_dlg_pink_light"
                android:text="CANCEL"
                android:padding="5dp"
                android:textSize="14sp"
                android:textColor="@android:color/white"
                android:textStyle="bold" />
        </TableRow>
    </TableLayout>

</LinearLayout>

推荐答案

在drawable中创建一个XML文件,例如dialog_bg.xml:

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

将其设置为布局XML中的背景:

android:background="@drawable/dialog_bg"

将对话框根视图的背景设置为透明,因为Android将对话框布局放在根视图中,该根视图隐藏了自定义布局中的角落.

Java:

dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));

Kotlin:

dialog.window?.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))

Android相关问答推荐

CameraX与jetpack组成屏幕逻辑

房间DB:UPSERT返回什么?

如何在Jetpack Compose中更新异步回调的用户界面

OutlinedTextField仅显示一次

不能有意地从一个活动的可组合功能转移到另一个活动

Android写/读二进制文件到共享存储

如何在喷气背包中绕过集装箱

React-Native Manifest 合并失败并出现多个错误

了解 CoroutineScope(Job() + Dispatchers.Main) 语法

Android Studio Relay插件(版本0.3.07)错误

围绕动态大小的内容包装 Jetpack Compose Row?

任务 ':app:checkReleaseDuplicateClasses' 执行失败

如何以编程方式通过 whatsapp android 共享图像和文本

组成不重叠的元素

前台服务通知需要几秒钟才能显示

Material Design 网站的新设计已启动,但我找不到实现选项卡,该选项卡在哪里?

Android 自动分页如何与 media3 配合使用?

使用 Jetpack Compose 时,如何以简单的方式在 Color.kt 中定义 colored颜色 ?

这是 let 函数的正确用法吗?

当我更改 ViewModel var 时,Kotlin + Compose 中的 Composable 不会更新