[英]Unable to remove dialog background
你好,我正在嘗試刪除我的對話框的背景
getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
它工作得很好
但是在為我的對話框實現陰影效果后,即使背景可繪制對象設置為透明,這個問題也會再次出現
這是我添加的行
getDialog().getWindow().setBackgroundDrawableResource(android.R.drawable.dialog_holo_light_frame);
這是它的截圖
完整代碼
sort_image_dialog_fragment.java
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);
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;
}
}
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>
與布局無關的問題; 因為您分配給對話框的任何布局; 對話框將其應用於其窗口; 並且對話框窗口默認為白色矩形(即沒有圓角)。
因此,您在角落看到的白色與對話窗口背景有關。
所以,為了制作圓角對話框; 您必須通過以下 2 個步驟在其窗口上更改它:
第 1 步:在 themes.xml/styles.xml 中創建一個刪除對話框窗口背景的主題:
@空值通過覆蓋自定義DialogFragment
類中的 getTheme() 來應用此主題:
@Override
public int getTheme() {
return R.style.NoBackgroundDialogTheme;
}
res\\drawable\\rounded_background.xml:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners android:radius="20dp" />
</shape>
使用view.setBackgroundResource()
將此應用到 dialogFragment 視圖並刪除您在onCreateView()
提供的其他窗口調用:
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.rounded_background);
return view;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.