序言
BottomSheetDialog 默认样式有各种边距。于是选了一个简单的dialog。记录一下。怎么去除默认样式。达到设计图的标准。
设计图
代码
DeleteImageDialog
public class DeleteImageDialog extends BottomSheetDialog {
public DeleteImageDialog(@NonNull Context context) {
super(context);
}
CallBack<Void> callBack;
public void setCallBack(CallBack<Void> callBack) {
this.callBack = callBack;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.delete_image_dialog);
fullScreen();
initViews();
}
private void initViews() {
findViewById(R.id.tv_cancel).setOnClickListener(v->{
dismiss();
});
findViewById(R.id.tv_delete).setOnClickListener(v->{
if(callBack!=null){
callBack.call(null);
}
dismiss();
});
}
void fullScreen() {
//设置在底部显示
getWindow().setGravity(Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL);
//清除decorView的背景色
getWindow().getDecorView().setBackground(new ColorDrawable(Color.TRANSPARENT));
getWindow().findViewById(R.id.design_bottom_sheet).setBackgroundColor(Color.TRANSPARENT);
//清除decorView的padding
getWindow().getDecorView().setPadding(0, 0, 0, 0);
getWindow().getAttributes().width = ViewGroup.LayoutParams.MATCH_PARENT;
//在小米机型中,必须把高度设置为MATCH_PARENT,否则会抖动。
getWindow().getAttributes().height = ViewGroup.LayoutParams.MATCH_PARENT;
}
}
布局文件
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
tools:background="#333"
android:layout_height="match_parent"
tools:ignore="MissingDefaultResource">
<RelativeLayout
android:layout_gravity="bottom"
android:background="@drawable/delete_image_dialog_bottom_bg"
android:layout_width="match_parent"
android:layout_height="156dp">
<TextView
android:id="@+id/tv_delete"
android:layout_marginTop="38dp"
android:layout_centerHorizontal="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="删除图片"
android:textColor="#fffd2d3b"
android:textSize="16sp"
/>
<View
android:layout_marginTop="71dp"
android:layout_marginStart="15dp"
android:layout_marginEnd="15dp"
android:layout_width="match_parent"
android:background="#F4F4F4"
android:layout_height="1dp"/>
<TextView
android:id="@+id/tv_cancel"
android:background="@drawable/delete_image_dialog_title_bg"
android:gravity="center"
android:textSize="16sp"
android:layout_marginStart="15dp"
android:layout_marginEnd="15dp"
android:layout_marginBottom="25.5dp"
android:text="取消"
android:layout_alignParentBottom="true"
android:layout_width="match_parent"
android:layout_height="44dp"
tools:ignore="HardcodedText" />
</RelativeLayout>
</FrameLayout>
背景
delete_image_dialog_bottom_bg
<?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="10dp"
android:topRightRadius="10dp" />
</shape>
delete_image_dialog_title_bg
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:width="345dp" android:height="44dp">
<shape android:shape="rectangle">
<solid android:color="#fff7f7f7" />
<corners android:radius="22dp" />
</shape>
</item>
</selector>