我有一个自定义的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);
因为它覆盖了底部对话框的默认背景,底部视图上方不会有任何半透明的灰色。
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));
添加圆角形状,使其为你的根布局的背景
<?xml version="1.0" encoding="utf-8" ?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners
android:topLeftRadius="@dimen/padding_margin_16_dp"
android:topRightRadius="@dimen/padding_margin_16_dp" />
<solid android:color="@color/white" />
</shape>
在你的BottomSheetDialogFragment上使背景透明
override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState)
(view?.parent as View).setBackgroundColor(Color.TRANSPARENT)
}
它的工作为约束布局,框架布局,线性布局,相对布局。
首先,你应该创建一个可绘制的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>
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));
步骤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格式的背景。