android - 在android中删除圆角外的自定义对话框颜色
问题描述
我正在尝试将圆角应用于我的自定义对话框。对话框正确显示圆角,但角的外部空间变为白色。
可绘制/dialog_message_background
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid
android:color="?postCardBackground"/> //light black color
<corners
android:radius="30dp" />
<padding
android:left="10dp"
android:top="10dp"
android:right="10dp"
android:bottom="10dp" />
</shape>
我的布局
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
android:background="@drawable/dialog_message_background"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:background="@drawable/dialog_message_background"
android:orientation="vertical"
android:gravity="center"
android:layout_centerInParent="true"
android:layout_width="250dp"
android:layout_height="wrap_content">
<TextView
android:id="@+id/dialogMessageTv"
android:text="Error occured"
android:maxLines="3"
android:textStyle="bold"
android:textColor="?attr/postUserNameColor"
android:textSize="18sp"
android:layout_gravity="center"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<ImageView
android:layout_marginTop="20dp"
android:id="@+id/messageDialogImg"
android:layout_width="70dp"
android:layout_height="70dp" />
<Button
android:id="@+id/messageDialogOkBtn"
android:layout_marginTop="20dp"
android:layout_marginBottom="5dp"
android:background="@drawable/dark_purple_btn_back"
android:text="ok"
android:textStyle="bold"
android:textSize="18sp"
android:textColor="@color/white"
android:layout_width="150dp"
android:layout_height="40dp" />
</LinearLayout>
</RelativeLayout>
输出窗口
帮我去除自定义对话框外的白色。
解决方案
您不需要形状来实现Dialog
圆角。您可以覆盖您的getTheme()
方法DialogFragment
:
import androidx.fragment.app.DialogFragment
class RoundedDialog: DialogFragment() {
override fun getTheme() = R.style.RoundedCornersDialog
//....
}
和:
<style name="RoundedCornersDialog" parent="@style/Theme.MaterialComponents.Dialog">
<item name="dialogCornerRadius">16dp</item>
</style>
在 Java 中只需使用
public class RoundedDialog extends DialogFragment {
@Override
public int getTheme() {
return R.style.RoundedCornersDialog;
}
}
推荐阅读
- bash - Dockerfile RUN 命令是否在登录 shell 环境中运行?
- node.js - 更新 mongodb 中数组对象数组的嵌入文档
- python - 将多个 json 对象放入一个列表中
- javascript - 从函数内的数组中获取随机元素
- sql-server - 如何使用 azure 流分析根据输出/blob 存储中的数据仅插入不同的行?
- javascript - 至少包含一个字符的正则表达式。双引号成对。不应该包含@和?
- c - 安卓上的 Openmp
- css - 在浏览器中获取文档对象模型更改 (Diff) 以修改 CSS
- c# - 从大量十进制数(包括负数)中查找具有给定总和的所有可能子集
- c# - 如何在 cake 中编译 .netcore 2.2 项目