如何通过代码绕过AlertDialog的角落

时间:2019-04-15 19:23:07

标签: android layout

我有以下代码,这为我创建了一个包含Web视图的Alertdialog。 如何设置代码圆角?

public QuickTextViewer(Context context){
    this.context = context;
    webView = new WebView(context);
    webView.setScrollBarStyle(View.SCROLLBARS_OUTSIDE_OVERLAY);
    webView.loadData("No Text", "text/html", "utf-8");
    webView.setWebViewClient(new WebViewClient() {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);
            return true;
        }
    });
    alert = new AlertDialog.Builder(context);
    webView.setBackgroundColor(Color.WHITE);
    alert.setView(webView);

    // Dialog
    dialog = alert.create();
    dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
    Window window = dialog.getWindow();
    window.setFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL, WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL);
    window.clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
    window.setGravity(Gravity.RIGHT);
    dialog.getWindow().setGravity(Gravity.END);
    dialog.setCancelable(true);
    dialog.setCanceledOnTouchOutside(true);

}

2 个答案:

答案 0 :(得分:4)

您可以使用MaterialAlertDialogBuilder进行此操作,该ShapeAppearanceModel使用材料规格配置实例化的AlertDialog

您可以应用自定义enter image description here
像这样:

 MaterialAlertDialogBuilder materialAlertDialogBuilder = 
         new MaterialAlertDialogBuilder(context).setTitle("Dialog")
            .setMessage("Lorem ipsum dolor ....")
            .setPositiveButton("Ok", /* listener = */ null)
            .setNegativeButton("Cancel", /* listener = */ null);

 float radius = getResources().getDimension(R.dimen.default_corner_radius);

 // Set a custom ShapeAppearanceModel
 MaterialShapeDrawable alertBackground = (MaterialShapeDrawable) materialAlertDialogBuilder.getBackground();
 alertBackground.setShapeAppearanceModel(
          alertBackground.getShapeAppearanceModel()
          .toBuilder()
          .setAllCorners(CornerFamily.ROUNDED,radius16)
          .build());

 materialAlertDialogBuilder.show();

this

它需要版本 1.1.0 (当前最新版本为1.1.0-beta02)。

答案 1 :(得分:0)

这可能是很长的路要走,但这是完美的解决方案,创建一个新的可绘制文件并编写此代码

circle.xml(存储在可绘制文件中)

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

<corners
    android:radius="100dp"
    />
<solid
    android:color="#E6E4E0"
    />
<stroke
    android:width="3dp"
    android:color="#B48B05"/>

然后,为您的Dialog布局创建一个布局文件,并将上面的文件用作背景

 android:background="@drawable/circle"

现在创建一个对话框类来实现您的布局

DialogClass.java

import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.provider.Settings;
import android.view.View;
import android.view.Window;
import android.widget.Button;

public class DialogClass extends Dialog
        implements  android.view.View.OnClickListener{

public Activity c;
public Dialog d;
public Button SETTINGS, OK;

public No_Internet(Activity a) {
    super(a);
    this.c = a;
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.dialoglayout);
    SETTINGS = findViewById(R.id.btnsettings);
    OK = findViewById(R.id.btnOK);

    SETTINGS.setOnClickListener(this);
    OK.setOnClickListener(this);

}

@Override
public void onClick(View v) {
    switch (v.getId()) {

        case R.id.btnOK:
            dismiss();
            break;

        case R.id.btnsettings:
            Intent intent = new Intent(Settings.ACTION_SETTINGS);
            c.startActivity(intent);
            break;
    }
    dismiss();
    } 
 }

因此,当您要使用此对话框时,只需使用此

 DialogClass dialog = new DialogClass(activityName.this);
 internet.show();