[英]How to remove the shadow created by bottom sheet dialog fragment in android?
[英]Remove White Background in Bottom Sheet Dialog Fragment
如何刪除底部工作表對話框片段中的白色背景?
我試圖在回答這個或設置在布局XML透明背景,但仍得到這樣的結果
這是我的代碼
public class BottomSheetFragment extends BottomSheetDialogFragment {
private Record record;
private MainFragment fragment;
public static BottomSheetFragment getInstance() {
return new BottomSheetFragment ();
}
public BottomSheetFragment setRecord(Record record) {
this.record = record;
return this;
}
public BottomSheetFragment setFragment(MainFragment fragment) {
this.fragment = fragment;
return this;
}
@TargetApi(Build.VERSION_CODES.O)
@Override
public void onViewCreated(View view, @Nullable final Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);
getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT);
setStyle(DialogFragment.STYLE_NO_FRAME, android.R.style.Theme);
//Set content
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.layout_bottom_sheet, container);
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
}
layout_bottom_sheet.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/cardView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="60dp"
android:background="@drawable/bg_cardboard">
</FrameLayout>
您需要為底部工作表視圖本身設置透明背景。
下面是 Kotlin 的一個例子:
class YourBottomSheetFragment : BaseBottomSheetDialogFragment() {
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
val dialog = super.onCreateDialog(savedInstanceState) as BottomSheetDialog
dialog.setOnShowListener { setupBottomSheet(it) }
return dialog
}
private fun setupBottomSheet(dialogInterface: DialogInterface) {
val bottomSheetDialog = dialogInterface as BottomSheetDialog
val bottomSheet = bottomSheetDialog.findViewById<View>(
com.google.android.material.R.id.design_bottom_sheet)
?: return
bottomSheet.setBackgroundColor(Color.TRANSPARENT)
}
}
使用此主題更改對話框的背景顏色
<style name="MyDialog" parent="@android:style/Theme.Dialog">
<item name="android:windowFrame">@null</item>
<item name="android:windowBackground">Your custom color here</item>
<item name="android:windowIsFloating">false</item>
<item name="android:windowContentOverlay">@null</item>
<item name="android:windowTitleStyle">@null</item>
<item name="android:colorBackgroundCacheHint">@null</item>
<item name="android:windowAnimationStyle">@android:style/Animation.Dialog</item>
<item name="android:windowSoftInputMode">stateUnspecified|adjustPan</item>
<item name="android:gravity">center</item>
</style>
並且您的onViewCreated
應該像或只添加setStyle(DialogFragment.STYLE_NO_TITLE, R.style.MyDialog);
@Override
public void onViewCreated(View view, @Nullable final Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);
getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT);
setStyle(DialogFragment.STYLE_NO_TITLE, R.style.MyDialog);
//Set content
}
您必須為底部工作表對話框片段覆蓋“onCreateDialog”函數,就像在 kotlin 中這樣:
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
val dialog = super.onCreateDialog(savedInstanceState)
dialog.setOnShowListener {
//this line transparent your dialog background
(view?.parent as ViewGroup).background =
ColorDrawable(Color.TRANSPARENT)
}
return dialog
}
對我來說,它以編程方式使背景透明:
((View) viewBotaoSheet.getParent()).setBackgroundColor(getResources().getColor(android.R.color.transparent))
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
((View) getView().getParent()).setBackgroundColor(Color.TRANSPARENT);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.