向警报对话框添加圆角

我正在尝试向 中添加圆角AlertDialog,但我不明白形状文件的逻辑(不起作用)。我将它用作 的背景RelativeLayout,AlertDialog但它似乎被忽略了。这是形状文件:


<?xml version="1.0" encoding="utf-8"?>

<shape xmlns:android="http://schemas.android.com/apk/res/android"

    android:shape="rectangle">

    <solid android:color="@android:color/transparent"/>

    <corners android:radius="10dp" />

    <padding android:left="10dp" android:right="10dp"/>

</shape>

这是警报对话框 xml:


<RelativeLayout

    xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:tools="http://schemas.android.com/tools"

    android:id="@+id/dialog_rl"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:padding="10dp"

    tools:context=".UserList"

    android:background="@drawable/shape_dialog">

    <TextView

        android:id="@+id/dialog_titile"

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:text="Scegli un'operazione"

        android:textAlignment="center"

        android:padding="5dp"

        android:textColor="@android:color/black"

        android:background="#D3D3D3"

        android:textSize="26dp" />


    <TextView

        android:id="@+id/dialog_tv"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:text="Vuoi aprire o eliminare il test?"

        android:textAlignment="center"

        android:padding="15dp"

        android:textSize="26dp"

        android:layout_marginLeft="80dp"

        android:background="@android:color/white"

        android:textColor="@android:color/black"

        android:layout_below="@id/dialog_titile" />

    <Button

        android:id="@+id/dialog_neutral_btn"

        android:layout_width="80dp"

        android:layout_height="wrap_content"

        android:text="Indietro"

        android:layout_below="@id/dialog_tv"

        android:textColor="@android:color/black"

        android:background="@drawable/button_bg_3" />



这就是结果 我错过了什么?



www说
浏览 183回答 5
5回答

慕无忌1623718

只需使用官方Material Components for Android 库中包含的官方Material AlertDialog。new MaterialAlertDialogBuilder(context)             .setTitle("Title")             .setMessage("Message")             .setPositiveButton("Ok", null)             .show();并使用主题<item name="materialAlertDialogTheme">@style/ThemeOverlay.MaterialComponents.MaterialAlertDialog</item>它遵循以下准则:您可以使用属性自定义组件的形状shapeAppearanceOverlay。就像是:<!-- Alert Dialog -->  <style name="MyThemeOverlayAlertDialog" parent="@style/ThemeOverlay.MaterialComponents.MaterialAlertDialog">    <item name="shapeAppearanceOverlay">@style/ShapeAppearanceOverlay.MyApp.Dialog.Rounded</item>  </style>  <style name="ShapeAppearanceOverlay.MyApp.Dialog.Rounded" parent="">    <item name="cornerFamily">rounded</item>    <item name="cornerSize">8dp</item>  </style>

陪伴而非守候

我已经检查了你的形状,它在我的手机上看起来不错,我有这个形状以同样的方式工作 + 有渐变:<shape&nbsp;xmlns:android="http://schemas.android.com/apk/res/android"android:shape="rectangle"> <gradient &nbsp;&nbsp;&nbsp;&nbsp;android:angle="-90" &nbsp;&nbsp;&nbsp;&nbsp;android:centerColor="#F2F2F2" &nbsp;&nbsp;&nbsp;&nbsp;android:endColor="#ADA996" &nbsp;&nbsp;&nbsp;&nbsp;android:startColor="#DBDBDB"&nbsp;/> <stroke &nbsp;&nbsp;&nbsp;&nbsp;android:width="2dp" &nbsp;&nbsp;&nbsp;&nbsp;android:color="#000000"&nbsp;/> <corners&nbsp;android:radius="8dp"&nbsp;/> <padding &nbsp;&nbsp;&nbsp;&nbsp;android:bottom="4dp" &nbsp;&nbsp;&nbsp;&nbsp;android:left="4dp" &nbsp;&nbsp;&nbsp;&nbsp;android:right="4dp" &nbsp;&nbsp;&nbsp;&nbsp;android:top="4dp"&nbsp;/> </shape>尝试使用上面的形状,如果它无论如何都不起作用,请尝试在单个按钮上使用您的形状 - 如果您看到您的形状正常工作,您就会知道您的问题来自您的布局文件。也许您需要做的就是Invalidate Caches/Restart,您可能没有错误,但您的应用程序正在使用来自缓存的旧形状

SMILET

您可以通过执行以下步骤来实现。您必须为 Textview 和主布局创建两个可绘制形状的 xml1. border_no_white_bg.xml<?xml version="1.0" encoding="utf-8"?><shape xmlns:android="http://schemas.android.com/apk/res/android"><solid android:color="@color/white" /><stroke&nbsp; &nbsp; android:width="1dp"&nbsp; &nbsp; android:color="@color/white" />&nbsp; <corners android:radius="6dp" /></shape>2. shape_button_orange_bg_with_radius.xml<?xml version="1.0" encoding="utf-8"?>&nbsp;<shape xmlns:android="http://schemas.android.com/apk/res/android"><stroke&nbsp; &nbsp; android:width="1dp"&nbsp; &nbsp; android:color="@color/black" />&nbsp; <corners android:radius="15dp" /></shape>3. Now in main_popup.xml, replace by below code<?xml version="1.0" encoding="utf-8"?><LinearLayout&nbsp; &nbsp; android:layout_width="match_parent"&nbsp; &nbsp; android:layout_height="wrap_content"&nbsp; &nbsp; android:layout_centerInParent="true"&nbsp; &nbsp; android:layout_margin="15dp"&nbsp; &nbsp; android:background="@drawable/border_no_white_bg"&nbsp; &nbsp; android:orientation="vertical">&nbsp; &nbsp; <TextView&nbsp; &nbsp; &nbsp; &nbsp; android:layout_width="match_parent"&nbsp; &nbsp; &nbsp; &nbsp; android:layout_height="wrap_content"&nbsp; &nbsp; &nbsp; &nbsp; android:layout_gravity="center_horizontal"&nbsp; &nbsp; &nbsp; &nbsp; android:layout_margin="10dp"&nbsp; &nbsp; &nbsp; &nbsp; android:background="#CCCCCC"&nbsp; &nbsp; &nbsp; &nbsp; android:baselineAligned="false"&nbsp; &nbsp; &nbsp; &nbsp; android:gravity="center"&nbsp; &nbsp; &nbsp; &nbsp; android:padding="10dp"&nbsp; &nbsp; &nbsp; &nbsp; android:text="Scegli un'operazione"&nbsp; &nbsp; &nbsp; &nbsp; android:textColor="#000"&nbsp; &nbsp; &nbsp; &nbsp; android:textSize="18sp" />&nbsp; &nbsp; <TextView&nbsp; &nbsp; &nbsp; &nbsp; android:layout_width="match_parent"&nbsp; &nbsp; &nbsp; &nbsp; android:layout_height="wrap_content"&nbsp; &nbsp; &nbsp; &nbsp; android:layout_gravity="center_horizontal"&nbsp; &nbsp; &nbsp; &nbsp; android:layout_margin="10dp"&nbsp; &nbsp; &nbsp; &nbsp; android:baselineAligned="false"&nbsp; &nbsp; &nbsp; &nbsp; android:gravity="center"&nbsp; &nbsp; &nbsp; &nbsp; android:padding="10dp"&nbsp; &nbsp; &nbsp; &nbsp; android:text="Vuoi aprire o eliminare il test?"&nbsp; &nbsp; &nbsp; &nbsp; android:textColor="#000"&nbsp; &nbsp; &nbsp; &nbsp; android:textSize="18sp" />&nbsp; &nbsp; <RelativeLayout&nbsp; &nbsp; &nbsp; &nbsp; android:layout_width="wrap_content"&nbsp; &nbsp; &nbsp; &nbsp; android:layout_height="wrap_content"&nbsp; &nbsp; &nbsp; &nbsp; android:layout_marginBottom="15dp"&nbsp; &nbsp; &nbsp; &nbsp; android:orientation="horizontal">&nbsp; &nbsp; &nbsp; &nbsp; <Button&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:layout_width="wrap_content"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:layout_height="40dp"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:layout_marginLeft="15dp"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:layout_marginRight="15dp"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:background="@drawable/shape_button_orange_bg_with_radius"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:text="Indietro"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:textColor="#000"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:textSize="14sp" />&nbsp; &nbsp; &nbsp; &nbsp; <LinearLayout&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:layout_width="wrap_content"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:layout_height="wrap_content"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:layout_alignParentRight="true"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:orientation="horizontal">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <Button&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:layout_width="wrap_content"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:layout_height="40dp"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:layout_marginLeft="5dp"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:layout_marginRight="5dp"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:background="@drawable/shape_button_orange_bg_with_radius"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:text="Apri"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:textColor="#000"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:textSize="14sp" />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <Button&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:layout_width="wrap_content"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:layout_height="40dp"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:layout_marginLeft="5dp"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:layout_marginRight="15dp"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:background="@drawable/shape_button_orange_bg_with_radius"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:text="Elimina"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:textColor="#000"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:textSize="14sp" />&nbsp; &nbsp; &nbsp; &nbsp; </LinearLayout>&nbsp; &nbsp; </RelativeLayout>&nbsp; &nbsp; &nbsp;</LinearLayout>&nbsp;</RelativeLayout>4. popup_window_animation&nbsp; &nbsp;Inside res->values->style.xml, add this code<style name="popup_window_animation">&nbsp; &nbsp; <item name="android:windowEnterAnimation">@anim/fade_in</item>&nbsp; &nbsp; <item name="android:windowExitAnimation">@anim/fade_out</item></style>现在创建一个方法来调用这个布局public void showPopup(View anchorView) {&nbsp; &nbsp; final View popupView = getLayoutInflater().inflate(R.layout.main_popup, null);&nbsp; &nbsp; RelativeLayout layout_close;&nbsp; &nbsp; // Declare your views here&nbsp; &nbsp; final PopupWindow popupWindow = new PopupWindow(popupView, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);&nbsp; &nbsp; popupWindow.setAnimationStyle(R.style.popup_window_animation);&nbsp; &nbsp; layout_feedback_close = (RelativeLayout) popupView.findViewById(R.id.layout_feedback_close);&nbsp; &nbsp; // Here find view by ids&nbsp; &nbsp; layout_feedback_close.setOnClickListener(new View.OnClickListener() {&nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; public void onClick(View v) {&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; });&nbsp; &nbsp; popupWindow.setFocusable(true);&nbsp; &nbsp; popupWindow.setBackgroundDrawable(new ColorDrawable());&nbsp; &nbsp; int location[] = new int[2];&nbsp; &nbsp; anchorView.getLocationOnScreen(location);&nbsp; &nbsp; popupWindow.showAtLocation(anchorView, Gravity.CENTER, location[0], location[1] + anchorView.getHeight());&nbsp; &nbsp; &nbsp;}

繁花不似锦

你的 shape_dialog.xml 应该是这样的<?xml version="1.0" encoding="utf-8"?>&nbsp;<shape xmlns:android="http://schemas.android.com/apk/res/android">&nbsp; &nbsp;<solid&nbsp; &nbsp; &nbsp;android:color="@color/white"/>&nbsp; &nbsp;<corners&nbsp; &nbsp; &nbsp; android:radius="30dp" />&nbsp; &nbsp;<padding&nbsp; &nbsp; android:left="10dp"&nbsp; &nbsp; android:top="10dp"&nbsp; &nbsp; android:right="10dp"&nbsp; &nbsp; android:bottom="10dp" /></shape>您需要将对话框的背景设置为透明状。dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));

子衿沉夜

使用DialogClass 而不是AlertDialogBuilder.代码片段showDialog()使用 Dialog 类而不是定义此方法AlertBuilderpublic void showDialog() {&nbsp; &nbsp; final Dialog dialog = new Dialog(this);&nbsp; &nbsp; dialog.setContentView(R.layout.your_xml);&nbsp; &nbsp; Button dialog_neutral_btn = dialog.findViewById(R.id.dialog_neutral_btn);&nbsp; &nbsp; Button dialog_positive_btn = dialog.findViewById(R.id.dialog_positive_btn);&nbsp; &nbsp; Button dialog_negative_btn=dialog.findViewById(R.id.dialog_negative_btn);&nbsp; &nbsp; TextView dialog_titile = dialog.findViewById(R.id.dialog_titile);&nbsp; &nbsp; TextView dialog_tv = dialog.findViewById(R.id.dialog_tv);&nbsp; &nbsp;// do you stuff here , define click listeners&nbsp; &nbsp; dialog.show();}用法&nbsp;yourButton.setOnClickListener(new View.OnClickListener() {&nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; public void onClick(View v) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;showDialog();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; });笔记:-android.support.v7.app.AlertDialog代替android.app.AlertDialog如果您想使用 AlertDialogBuilder
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java