我有一个自定义的bittomsheetdialogfragment,我想在底部视图的顶部有圆角
这是我的自定义类,它膨胀了我想要从底部显示的布局
View mView;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
mView = inflater.inflate(R.layout.charge_layout, container, false);
initChargeLayoutViews();
return mView;
}
我还有这个XML资源文件作为背景:
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle"
>
<corners android:topRightRadius="35dp"
android:topLeftRadius="35dp"
/>
<solid android:color="@color/white"/>
<padding android:top="10dp"
android:bottom="10dp"
android:right="16dp"
android:left="16dp"/>
</shape>
问题是,当我把这个资源文件设置为我的布局的根元素的背景,角仍然不是圆角。
我不能使用以下代码:
this.getDialog().getWindow().setBackgroundDrawableResource(R.drawable.charge_layout_background);
因为它覆盖了底部对话框的默认背景,底部视图上方不会有任何半透明的灰色。
步骤1:
创建一个命名为rounded_background.xml的res/drawable:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners
android:topLeftRadius="32dp"
android:topRightRadius="32dp" />
<solid android:color="#D81B60" />
</shape>
步骤2:
创建这个样式来删除对话框背景:
<style name="NoBackgroundDialogTheme" parent="Theme.AppCompat.Light.Dialog">
<item name="android:windowBackground">@null</item>
</style>
步骤3:
使用setBackgroundResource()将可绘制对象设置为对话框的根视图,并通过覆盖getTheme()方法设置样式
Java:
public class MyDialogFragment extends BottomSheetDialogFragment {
@Override
public int getTheme() {
return R.style.NoBackgroundDialogTheme;
}
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = View.inflate(requireContext(), R.layout.bottom_sheet_profile, null);
view.setBackgroundResource(R.drawable.rounded_background);
return view;
}
}
科特林:
class MyDialogFragment : BottomSheetDialogFragment() {
override fun getTheme() = R.style.NoBackgroundDialogTheme
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View {
val view: View = View.inflate(requireContext(), R.layout.bottom_sheet_profile, null)
view.setBackgroundResource(R.drawable.rounded_background)
return view
}
}
结果:
如果(view.setBackgroundResource(R.drawable.rounded_background))这一行不能工作,然后尝试设置片段的Xml格式的背景。
这个答案只针对背景色设置为彩色的问题。透明后,设置一个绘图与圆形背景的布局。
没有一个答案能让我把背景色设置为“彩色”。透明的,除非重写setupDialog()解决方案:
@Override
public void setupDialog(Dialog dialog, int style) {
super.setupDialog(dialog, style);
View contentView = View.inflate(getContext(),
R.layout.fragment_bottom_sheet, null);
dialog.setContentView(contentView);
...
((View) contentView.getParent()).setBackgroundColor(ContextCompat.getColor(getContext(), android.R.color.transparent));
}
但是你在这里为对话框设置的contentView不是你在onCreateView()中膨胀时在onViewCreated()中获得的视图。它打破了标准流程,所以可能会出现问题,比如你不能使用视图绑定- onViewCreated()中的Kotlin Android扩展
所以我稍微调整了一下设置onActivityCreated()的背景:
override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState)
(view?.parent as View).setBackgroundColor(Color.TRANSPARENT)
}
希望这对遇到同样麻烦的人有所帮助
Create a shape drawable .. which we will use as background for bottom sheet .
Provide the appropriate value for radius of top left and right corner .
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners
android:topLeftRadius="24dp"
android:topRightRadius="24dp" />
<padding android:top="2dp" />
<solid android:color="@color/white" />
</shape>
Now create style for " Bottom sheet dialog fragment "
<style name="BottomSheet" parent="@style/Widget.Design.BottomSheet.Modal">
<item name="android:background">@drawable/drawable_bottomsheet_background</item>
</style>
<style name="BaseBottomSheetDialog" parent="@style/Theme.Design.Light.BottomSheetDialog">
<item name="android:windowIsFloating">false</item>
<item name="bottomSheetStyle">@style/BottomSheet</item>
</style>
<style name="BottomSheetDialogTheme" parent="BaseBottomSheetDialog" />
Now create a custom class that will extend BottomSheetDilogFragment ,where you provide your style .
open class CustomRoundBottomSheet : BottomSheetDialogFragment() {
override fun getTheme(): Int = R.style.BottomSheetDialogTheme
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog = BottomSheetDialog(requireContext(), theme)
}
Now use this class wherever you want to have round corner bottom sheet .
eg
class BottomSheetSuccess : CustomRoundBottomSheet() {
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
return inflater.inflate(R.layout.bottomsheet_shopcreate_success, container, false)
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
}
}
步骤1:
创建一个命名为rounded_background.xml的res/drawable:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners
android:topLeftRadius="32dp"
android:topRightRadius="32dp" />
<solid android:color="#D81B60" />
</shape>
步骤2:
创建这个样式来删除对话框背景:
<style name="NoBackgroundDialogTheme" parent="Theme.AppCompat.Light.Dialog">
<item name="android:windowBackground">@null</item>
</style>
步骤3:
使用setBackgroundResource()将可绘制对象设置为对话框的根视图,并通过覆盖getTheme()方法设置样式
Java:
public class MyDialogFragment extends BottomSheetDialogFragment {
@Override
public int getTheme() {
return R.style.NoBackgroundDialogTheme;
}
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = View.inflate(requireContext(), R.layout.bottom_sheet_profile, null);
view.setBackgroundResource(R.drawable.rounded_background);
return view;
}
}
科特林:
class MyDialogFragment : BottomSheetDialogFragment() {
override fun getTheme() = R.style.NoBackgroundDialogTheme
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View {
val view: View = View.inflate(requireContext(), R.layout.bottom_sheet_profile, null)
view.setBackgroundResource(R.drawable.rounded_background)
return view
}
}
结果:
如果(view.setBackgroundResource(R.drawable.rounded_background))这一行不能工作,然后尝试设置片段的Xml格式的背景。
我找到了一个简单的解决办法。
使用com.google.android.material:material:1.6.1
class MyBottomSheet: BottomSheetDialogFragment() {
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
disableShapeAnimation()
}
@SuppressLint("RestrictedApi", "VisibleForTests")
private fun disableShapeAnimation() {
try {
val dlg = dialog as BottomSheetDialog
dlg.behavior.disableShapeAnimations()
} catch (ex: Exception) {
Log.e("BaseBottomSheet", "disableShapeAnimation Exception:", ex)
}
}
}
这对我很管用。
创建一个背景绘图(例如命名为shape_rounded_dialog):
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="@color/color_white" />
<corners android:topLeftRadius="16dp"
android:topRightRadius="16dp" />
</shape>
添加下面的样式:
<style name="AppBottomSheetDialogTheme"
parent="Theme.MaterialComponents.Light.BottomSheetDialog">
<item name="bottomSheetStyle">@style/CustomBottomSheetStyle</item>
</style>
<style name="CustomBottomSheetStyle"
parent="Widget.Design.BottomSheet.Modal">
<item name="android:background">@drawable/shape_rounded_dialog</item>
</style>
在你的DialogFragment中,重写getTheme()方法来返回你的样式。
@Override
public int getTheme() {
return R.style.AppBottomSheetDialogTheme;
}