将圆角添加到警报对话框

时间:2019-08-03 08:24:12

标签: java android android-alertdialog android-dialog material-components-android

我试图在AlertDialog上添加圆角,但是我不了解形状文件的逻辑(不起作用)。我在RelativeLayout的{​​{1}}中将其用作背景,但似乎它被忽略了。这是形状文件:

AlertDialog

这是警报对话框xml:

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

And this is the result 我想念什么?

5 个答案:

答案 0 :(得分:1)

只需使用Android库的官方材料组件中包含的官方Material AlertDialog

new MaterialAlertDialogBuilder(context)
            .setTitle("Title")
            .setMessage("Message")
            .setPositiveButton("Ok", null)
            .show();

并使用主题

<item name="materialAlertDialogTheme">@style/ThemeOverlay.MaterialComponents.MaterialAlertDialog</item>

它遵循准则:

enter image description here

您可以使用 shapeAppearanceOverlay 属性customize the shape来使用组件。

类似的东西:

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

答案 1 :(得分:0)

使用Dialog类而不是AlertDialogBuilder

代码段

使用Dialog类而不是showDialog()来定义此方法AlertBuilder

public void showDialog() {

    final Dialog dialog = new Dialog(this);
    dialog.setContentView(R.layout.your_xml);

    Button dialog_neutral_btn = dialog.findViewById(R.id.dialog_neutral_btn);
    Button dialog_positive_btn = dialog.findViewById(R.id.dialog_positive_btn);
    Button dialog_negative_btn=dialog.findViewById(R.id.dialog_negative_btn);
    TextView dialog_titile = dialog.findViewById(R.id.dialog_titile);
    TextView dialog_tv = dialog.findViewById(R.id.dialog_tv);


   // do you stuff here , define click listeners

    dialog.show();
}

用法

 yourButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
           showDialog();
        }
    });

注意:-

android.support.v7.app.AlertDialog

代替

android.app.AlertDialog
  

如果您想使用AlertDialogBu​​ilder

答案 2 :(得分:0)

我检查了您的形状,并在手机上看起来不错,我的形状与+渐变效果相同:

<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient
    android:angle="-90"
    android:centerColor="#F2F2F2"
    android:endColor="#ADA996"
    android:startColor="#DBDBDB" />
<stroke
    android:width="2dp"
    android:color="#000000" />
<corners android:radius="8dp" />

<padding
    android:bottom="4dp"
    android:left="4dp"
    android:right="4dp"
    android:top="4dp" />
</shape>
  • 尝试使用上面的形状,如果仍然无法使用,请尝试在单个按钮上使用形状-如果您看到形状正确运行,您将知道问题出在布局文件中。

  • 也许您需要做的所有Invalidate Caches/Restart,可能没有错,但您的应用使用的是来自缓存的旧形状

答案 3 :(得分:0)

您的shape_dialog.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>

您需要设置对话框透明背景的背景。

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

答案 4 :(得分:0)

您可以通过执行以下步骤来实现。

您必须为Textview和主布局创建两个形状可绘制的xml

1. 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
    android:width="1dp"
    android:color="@color/white" />

  <corners android:radius="6dp" />
</shape>



2. shape_button_orange_bg_with_radius.xml

<?xml version="1.0" encoding="utf-8"?>
 <shape xmlns:android="http://schemas.android.com/apk/res/android">

<stroke
    android:width="1dp"
    android:color="@color/black" />
  <corners android:radius="15dp" />
</shape>

3. Now in main_popup.xml, replace by below code
<?xml version="1.0" encoding="utf-8"?>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    android:layout_margin="15dp"
    android:background="@drawable/border_no_white_bg"
    android:orientation="vertical">


    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:layout_margin="10dp"
        android:background="#CCCCCC"
        android:baselineAligned="false"
        android:gravity="center"
        android:padding="10dp"
        android:text="Scegli un'operazione"
        android:textColor="#000"
        android:textSize="18sp" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:layout_margin="10dp"
        android:baselineAligned="false"
        android:gravity="center"
        android:padding="10dp"
        android:text="Vuoi aprire o eliminare il test?"
        android:textColor="#000"
        android:textSize="18sp" />


    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="15dp"
        android:orientation="horizontal">


        <Button
            android:layout_width="wrap_content"
            android:layout_height="40dp"
            android:layout_marginLeft="15dp"
            android:layout_marginRight="15dp"
            android:background="@drawable/shape_button_orange_bg_with_radius"
            android:text="Indietro"
            android:textColor="#000"
            android:textSize="14sp" />


        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:orientation="horizontal">

            <Button
                android:layout_width="wrap_content"
                android:layout_height="40dp"
                android:layout_marginLeft="5dp"
                android:layout_marginRight="5dp"
                android:background="@drawable/shape_button_orange_bg_with_radius"
                android:text="Apri"
                android:textColor="#000"
                android:textSize="14sp" />

            <Button
                android:layout_width="wrap_content"
                android:layout_height="40dp"
                android:layout_marginLeft="5dp"
                android:layout_marginRight="15dp"
                android:background="@drawable/shape_button_orange_bg_with_radius"
                android:text="Elimina"
                android:textColor="#000"
                android:textSize="14sp" />
        </LinearLayout>

    </RelativeLayout>


     </LinearLayout>

 </RelativeLayout>


4. popup_window_animation
   Inside res->values->style.xml, add this code



<style name="popup_window_animation">
    <item name="android:windowEnterAnimation">@anim/fade_in</item>
    <item name="android:windowExitAnimation">@anim/fade_out</item>
</style>

现在创建一种方法来调用此布局

public void showPopup(View anchorView) {

    final View popupView = getLayoutInflater().inflate(R.layout.main_popup, null);

    RelativeLayout layout_close;
    // Declare your views here

    final PopupWindow popupWindow = new PopupWindow(popupView, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
    popupWindow.setAnimationStyle(R.style.popup_window_animation);

    layout_feedback_close = (RelativeLayout) popupView.findViewById(R.id.layout_feedback_close);
    // Here find view by ids

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

        }
    });

    popupWindow.setFocusable(true);
    popupWindow.setBackgroundDrawable(new ColorDrawable());
    int location[] = new int[2];
    anchorView.getLocationOnScreen(location);
    popupWindow.showAtLocation(anchorView, Gravity.CENTER, location[0], location[1] + anchorView.getHeight());
     }