BottomSheet设计异常:如何实现圆角及底部导航非透明效果?
解决BottomSheet圆角和底部导航栏透明问题
我来帮你搞定这两个问题,让你的BottomSheet和Google News这类应用的样式对齐!咱们分两步来处理:
一、给BottomSheet添加顶部圆角效果
首先需要创建一个带圆角的背景drawable,因为BottomSheet只需要顶部两个角有圆角,底部不需要。
- 在你的
drawable文件夹下新建bottom_sheet_rounded.xml,内容如下:
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> <solid android:color="@android:color/white" /> <!-- 这里设置BottomSheet的背景色,和你的主题匹配即可 --> <corners android:topLeftRadius="16dp" android:topRightRadius="16dp" /> <!-- 圆角大小可以根据需求调整 --> </shape>
- 修改你的
MyBottomSheet.xml,给根布局设置这个背景,同时开启裁剪属性确保圆角生效:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:id="@+id/bottom_sheet" android:elevation="10dp" android:minHeight="300dp" android:background="@drawable/bottom_sheet_rounded" android:clipToPadding="true" android:clipChildren="true" app:behavior_peekHeight="120dp" app:layout_behavior="android.support.design.widget.BottomSheetBehavior"> <ScrollView android:layout_width="match_parent" android:layout_height="match_parent"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:layout_marginBottom="30dp"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <!--controls here--> </LinearLayout> </LinearLayout> </ScrollView> </LinearLayout>
注意:
clipToPadding和clipChildren这两个属性很关键,它们能确保子控件不会超出圆角范围显示。
二、修复底部导航栏透明问题
BottomSheetDialog默认会将底部导航栏设置为透明,咱们只需要在初始化Dialog的时候手动设置导航栏颜色即可:
修改你的调用代码:
View view = LayoutInflater.Inflate(Resource.Layout.MyBottomSheet, null); BottomSheetDialog dialog = new BottomSheetDialog(this); dialog.SetContentView(view); // 获取Dialog的Window对象,设置导航栏颜色 Window window = dialog.Window; if (window != null) { // 设置导航栏颜色为你主题中定义的导航栏颜色,或者直接指定颜色值 window.SetNavigationBarColor(Resources.GetColor(Resource.Color.your_navigation_bar_color)); // 确保移除可能导致透明的Flag window.ClearFlags(WindowManagerFlags.TranslucentNavigation); } dialog.Show();
这里的
your_navigation_bar_color需要你在colors.xml中定义对应的颜色值,比如和你应用的底部导航栏颜色保持一致。
这样修改之后,你的BottomSheet就会有漂亮的顶部圆角,同时底部导航栏也会恢复正常的不透明状态啦!
内容的提问来源于stack exchange,提问作者Ali
相关产品推荐
相关产品推荐

