我有一个自定义的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);
因为它覆盖了底部对话框的默认背景,底部视图上方不会有任何半透明的灰色。
首先,你应该创建一个可绘制的XML文件,其中包含一个顶部圆角的形状,任何你想要的名称。
我将其命名为bottom rounded_top_shape.xml
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle"
>
<solid android:color="@android:color/white" />
<corners
android:topLeftRadius="16dp"
android:topRightRadius="16dp"
/>
然后在style.xml中添加这个
<style name="AppBottomSheetDialogTheme" parent="Theme.MaterialComponents.Light.BottomSheetDialog">
<item name="bottomSheetStyle">@style/AppModalStyle</item>
</style>
<style name="AppModalStyle" parent="Widget.Design.BottomSheet.Modal">
<item name="android:background">@drawable/rounded_top_shape</item>
</style>
然后在你的应用主题中添加如下所示的行
<style name="MyAppTheme" parent="Theme.MaterialComponents.Light.Bridge">
<!-- this line -->
<item name="bottomSheetDialogTheme">@style/AppBottomSheetDialogTheme</item>
</style>
简单的解决方案:
class TopRoundedCornersFragment : BottomSheetDialogFragment() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setStyle(STYLE_NORMAL, R.style.AppBottomSheetDialogTheme)
}
}
在styles.xml
<style name="BottomSheetStyle" parent="Widget.Design.BottomSheet.Modal">
<item name="android:background">@drawable/bottom_sheet_dialog_bg</item>
</style>
<style name="AppBottomSheetDialogTheme" parent="Theme.Design.Light.BottomSheetDialog">
<item name="bottomSheetStyle">@style/BottomSheetStyle</item>
</style>
最后,创建一个顶部圆角可绘制资源(bottom_sheet_dialog_bg.xml)
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="@android:color/white" />
<corners
android:topLeftRadius="4dp"
android:topRightRadius="4dp" />
</shape>
Koma Yip回答的另一个问题对我有用,你应该试试。
在drawable中创建一个xml,比如dialog_bg.xml
<?XML版本="1.0"编码="utf-8"?>
<形状xmlns: android = " http://schemas.android.com/apk/res/android " >
<固体android:颜色= " @color /白色" / >
<corners android:半径="30dp" />
<填充
android:左= " 10 dp”
android:顶级= " 10 dp”
android:对= " 10 dp”
android:底部= " 10 dp " / >
< / >形状
把这个放在你的布局XML根节点中:
将其设置为布局XML中的背景
android:背景= " @drawable / dialog_bg”
在onCreateView()中放入:
将对话框的背景设置为透明
dialog.getWindow()。setBackgroundDrawable(新ColorDrawable (Color.TRANSPARENT));
正如在其他答案中指出的,当state为BottomSheetBehavior时。STATE_EXPANDED角将被压平。
你可以通过设置BottomSheetBehavior的peekHeight属性并使用自定义样式来克服这个问题。
abstract class BaseBottomSheetFragment : BottomSheetDialogFragment(){
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
if (state == BottomSheetBehavior.STATE_EXPANDED) {
val displayMetrics = DisplayMetrics()
requireActivity().windowManager!!.defaultDisplay!!.getMetrics(displayMetrics)
(dialog as BottomSheetDialog).behavior.peekHeight = displayMetrics.heightPixels
} else {
(dialog as BottomSheetDialog).behavior.state = state
}
}
override fun getTheme(): Int {
return R.style.CustomBottomSheetDialog
}
}
CustomBottomSheetDialog风格
<style name="CustomBottomSheetDialog" parent="@style/ThemeOverlay.MaterialComponents.BottomSheetDialog">
<item name="bottomSheetStyle">@style/CustomBottomSheet</item>
<item name="materialButtonStyle">@style/CustomMaterialButtonStyle</item>
</style>
<style name="CustomMaterialButtonStyle" parent="@style/Widget.MaterialComponents.Button">
<item name="cornerRadius">@dimen/dialog_bottom_radius</item>
</style>
<style name="CustomBottomSheet" parent="Widget.MaterialComponents.BottomSheet">
<item name="shapeAppearanceOverlay">@style/CustomShapeAppearanceBottomSheetDialog</item>
</style>
<style name="CustomShapeAppearanceBottomSheetDialog" parent="">
<item name="android:background">@android:color/transparent</item>
<item name="backgroundTint">@android:color/transparent</item>
<item name="cornerFamily">rounded</item>
<item name="cornerSizeTopRight">@dimen/dialog_bottom_radius</item>
<item name="cornerSizeTopLeft">@dimen/dialog_bottom_radius</item>
<item name="cornerSizeBottomRight">0dp</item>
<item name="cornerSizeBottomLeft">0dp</item>
</style>
在你的BottomsheetDialogFragment类中添加这两个方法。
public void setDialogBorder(Dialog dialog) {
FrameLayout bottomSheet = (FrameLayout) dialog.getWindow().findViewById(android.support.design.R.id.design_bottom_sheet);
bottomSheet.setBackground(new ColorDrawable(Color.TRANSPARENT));
setMargins(bottomSheet, 10, 0, 10, 20);
}
private void setMargins(View view, int left, int top, int right, int bottom) {
if (view.getLayoutParams() instanceof ViewGroup.MarginLayoutParams) {
ViewGroup.MarginLayoutParams p = (ViewGroup.MarginLayoutParams) view.getLayoutParams();
p.setMargins(left, top, right, bottom);
view.requestLayout();
}
}
现在调用你的BottomsheetDialogFragment类的setupDialog()方法中的setDialogBorder(dialog)方法。
现在在可绘制文件夹中创建一个形状文件。
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners android:radius="20dp" />
<solid android:color="@color/white" />
<stroke
android:width="1dp"
android:color="@color/transparent" />
</shape>
现在在xml文件中设置父视图组对话框视图的背景。
android:background="@drawable/round_border_white"
完成了! !
如果您正在使用材质组件主题,您可以通过将背景颜色设置为透明,然后将布局的背景设置为四舍五入来覆盖BottomSheetDialog的默认行为。
<style name="Theme.MyApp" parent="Theme.MaterialComponents.DayNight.DarkActionBar">
<!-- Customize your theme here. -->
<item name="bottomSheetDialogTheme">@style/BottomSheetDialog</item>
</style>
<style name="BottomSheetDialog" parent="ThemeOverlay.MaterialComponents.BottomSheetDialog">
<item name="bottomSheetStyle">@style/bottomSheetStyleWrapper</item>
</style>
<style name="bottomSheetStyleWrapper" parent="Widget.MaterialComponents.BottomSheet.Modal">
<item name="android:backgroundTint">@android:color/transparent</item>
</style>
rounded_corner.xml:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="@color/white"/>
<corners android:topLeftRadius="26dp"
android:topRightRadius="26dp"/>
</shape>
然后将形状应用到你的底部表格根布局的背景:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/rounded_corner">
<!--
other layout components
-->
</LinearLayout>
注意,如果你在底部表单布局中使用Webview这样的组件,你可能无法实现预期的圆角,因为Webview有一个默认的白色背景。在这种情况下,你可以像这样简单地删除背景:
webView.setBackgroundColor(0);