【发布时间】:2021-12-19 09:47:11
【问题描述】:
您好,我正在尝试删除对话框的背景
getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
效果很好
但在为我的对话框实现阴影效果后,即使背景可绘制设置为透明,此问题也会再次出现
这是我添加的行
getDialog().getWindow().setBackgroundDrawableResource(android.R.drawable.dialog_holo_light_frame);
这是它的screenshot
完整代码
sort_image_dialog_fragment.java
public class sort_image_dialoge_fragment extends DialogFragment {
@Nullable
@org.jetbrains.annotations.Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable @org.jetbrains.annotations.Nullable ViewGroup container, @Nullable @org.jetbrains.annotations.Nullable Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
View view = inflater.inflate(R.layout.sort_image, container, false);
view.setBackgroundResource(R.drawable.dialog_rounded_background);
if (getDialog() != null && getDialog().getWindow() != null) {
getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
getDialog().getWindow().setBackgroundDrawableResource(android.R.drawable.dialog_holo_light_frame);
getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE);
getDialog().getWindow().setDimAmount(0.8f);
getDialog().getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
}
return view;
}
@Override
public int getTheme() {
return R.style.NoBackgroundDialogTheme;
}
}
sort_image.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.google.android.material.card.MaterialCardView
android:layout_width="300dp"
android:layout_height="200dp"
android:layout_gravity="center"
android:backgroundTint="@color/grey"
android:elevation="12dp"
app:cardCornerRadius="20dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/sort_textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerInParent="true"
android:layout_marginTop="5dp"
android:fontFamily="@font/roboto"
android:text="Sort by"
android:textAlignment="center"
android:textColor="@color/lite_grey"
android:textSize="25sp" />
<com.google.android.material.card.MaterialCardView
android:id="@+id/divider_line_sort_by"
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_below="@id/sort_textView"
android:layout_marginStart="10dp"
android:layout_marginTop="5dp"
android:layout_marginEnd="10dp"
android:backgroundTint="@color/lite_grey"
app:cardCornerRadius="50dp" />
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true">
<TextView
android:id="@+id/latest_sort_textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="30dp"
android:layout_marginTop="30dp"
android:fontFamily="@font/roboto"
android:text="Latest"
android:textAlignment="center"
android:textColor="@color/lite_grey"
android:textSize="25sp" />
<TextView
android:id="@+id/most_popular_sort_textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/latest_sort_textView"
android:layout_alignParentStart="true"
android:layout_centerInParent="true"
android:layout_marginStart="30dp"
android:layout_marginTop="25sp"
android:fontFamily="@font/roboto"
android:text="Most Popular"
android:textAlignment="center"
android:textColor="@color/lite_grey"
android:textSize="25sp" />
<RadioGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true">
<RadioButton
android:id="@+id/latest_sort_radio_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="23dp" />
<RadioButton
android:id="@+id/most_popular_sort_radio_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:checked="true" />
</RadioGroup>
</RelativeLayout>
</RelativeLayout>
</com.google.android.material.card.MaterialCardView>
</RelativeLayout>
【问题讨论】:
标签: android android-layout android-alertdialog android-dialogfragment customdialog