java - 将自定义边框添加到警报对话框生成器布局

标签 java android material-design androidx material-components-android

我正在尝试向我的警报对话框构建器添加带边框的自定义布局,但这是我得到的结果:

enter image description here

我为我的对话框创建了一个样式:

<style name="MyDialog" parent="@android:style/Theme.Dialog">
    <item name="android:background">@drawable/dialog_background</item>
</style>

这就是我定义它的方式:

<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<solid
    android:color="#00000000" />
<stroke
    android:width="5dp"
    android:background="#00ffffff" />
<corners android:radius="20dp" />

然后,在布局中我也定义了这一点:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="@dimen/_100sdp"
android:layout_height="@dimen/_80sdp"
android:gravity="center"
style="@style/MyDialog">

<CheckBox
    android:id="@+id/myCheckBox"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:layout_marginTop="@dimen/_10sdp"
    android:text="Abilitare la palette di colori?" />

<EditText
    android:id="@+id/nameEditText"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/genderRadioGroup"
    android:layout_alignParentLeft="true"
    android:ems="10"
    android:hint="Inserisci l'eta'">

    <requestFocus />
</EditText>

<RadioGroup
    android:id="@+id/genderRadioGroup"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/myCheckBox"
    android:checkedButton="@+id/maleRadioButton">


    <RadioButton
        android:id="@+id/maleRadioButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:text="Maschio" />


    <RadioButton
        android:id="@+id/femaleRadioButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/maleRadioButton"
        android:layout_alignParentLeft="true"
        android:text="Femmina" />
</RadioGroup>

1) 为什么确定按钮形状不正常? 2)由于无法使用 getWindow().setBackgroundDrawableResource(android.R.color.transparent); 方法,如何使背景透明? 这是警报的 Java 代码:

new AlertDialog.Builder(MainActivity.this).setView(formElementsView)
                    .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int id) {
                            InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
                            imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
                            getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
                            int selectedId = genderRadioGroup.getCheckedRadioButtonId();
                            RadioButton selectedRadioButton = (RadioButton) formElementsView.findViewById(selectedId);
                            Intent myIntent = new Intent(MainActivity.this, PaintingActivity.class);
                            myIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
                            if (myCheckBox.isChecked()) myIntent.putExtra("palette", "yes");
                            else myIntent.putExtra("palette", "no");
                            myIntent.putExtra("gender", selectedRadioButton.getText());
                            String eta = nameEditText.getText().toString();
                            if (eta.length()!=0) myIntent.putExtra("eta", eta);
                            else myIntent.putExtra("eta", "0");
                            myIntent.putExtra("protocollo", "a");
                            myIntent.putExtra("cornice", "1" + "");
                            myIntent.putExtra("userLogged", userLogged);
                            myIntent.putExtra("first", "yes");
                            MainActivity.this.startActivity(myIntent);
                            dialog.cancel();
                        }

                    }).show().getWindow().setLayout(600, 600);

最佳答案

有了新的Material Theme你可以customize the shape使用 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>

enter image description here

您还可以在主题中添加此属性来定义应用程序中所有对话框的样式:

<style name="Theme.MyApp" parent="Theme.MaterialComponents.Light"> 
    ...
    <item name="materialAlertDialogTheme">@style/MyThemeOverlayAlertDialog</item>
    ...
</style>

关于java - 将自定义边框添加到警报对话框生成器布局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57624137/

相关文章:

java - Spring 事务管理不适用于 Spring Boot + MyBatis?

android - 使用 android :layout_weight programmatically 填充 TableLayout

Android - 将色调背景颜色更改为抽屉导航?

android - Material Design 3 顶部应用栏没有阴影。如何启用它?

android - 使用 SupportToolbar 进行手机布局,使用独立操作栏进行平板电脑布局

java - Spring websockets - 分离 MessageMapping 和 SendTo

java - BigInteger 和 BigDecimal 的平方根和++ 运算符

java - 为什么使用 javabean 绑定(bind)属性而不是事件?

java - 当应用程序关闭时,我可以不断地从服务类中收听数据变化吗?

android - LinearLayout 重叠支持 CoordinatorLayout