사용자 지정 BttomSheetDialogFragment가 있고 Bottom View 상단에 둥근 모서리를 갖고 싶습니다.
이것은 내가 바닥에서 나타나고 싶은 레이아웃을 팽창시키는 내 Custom 클래스입니다.
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"/>
하지만 문제는이 리소스 파일을 내 레이아웃의 루트 요소의 배경으로 설정하면 모서리가 여전히 둥글 지 않다는 것입니다.
그리고 아래 코드를 사용할 수 없습니다.
this.getDialog().getWindow().setBackgroundDrawableResource(R.drawable.charge_layout_background);
BottomSheetDialog의 기본 배경을 재정의하고 하단 뷰 위에 반투명 회색 색상이 없기 때문에
답변
사용자 정의 드로어 블 만들기 rounded_dialog.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"/>
<corners android:topLeftRadius="16dp"
android:topRightRadius="16dp"/>
</shape>
그런 다음 드로어 블을 배경 bottomSheetDialogTheme
으로 styles.xml
사용하도록 재정의 합니다.
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="bottomSheetDialogTheme">@style/AppBottomSheetDialogTheme</item>
</style>
<style name="AppBottomSheetDialogTheme"
parent="Theme.Design.Light.BottomSheetDialog">
<item name="bottomSheetStyle">@style/AppModalStyle</item>
</style>
<style name="AppModalStyle"
parent="Widget.Design.BottomSheet.Modal">
<item name="android:background">@drawable/rounded_dialog</item>
</style>
그러면 앱의 모든 BottomSheetDialogs가 변경됩니다.
답변
새로운 Material Component 라이브러리를 사용하면 스타일 의 속성을 사용하여 구성 요소 의 모양 을 사용자 정의 할 수 있습니다shapeAppearanceOverlay
( 참고 : 버전 1.1.0 필요 ).
그냥 사용 BottomSheetDialogFragment
최우선 onCreateView
방법을 다음 바닥 시트 대화 상자에 대한 사용자 정의 스타일을 정의합니다.
앱 테마에서 bottomSheetDialogTheme
속성을 정의하십시오 styles.xml
.
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.MaterialComponents.Light">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
....
<item name="bottomSheetDialogTheme">@style/CustomBottomSheetDialog</item>
</style>
그런 다음 좋아하는 모양을 정의하십시오. shapeAppearanceOverlay
<style name="CustomBottomSheetDialog" parent="@style/ThemeOverlay.MaterialComponents.BottomSheetDialog">
<item name="bottomSheetStyle">@style/CustomBottomSheet</item>
</style>
<style name="CustomBottomSheet" parent="Widget.MaterialComponents.BottomSheet">
<item name="shapeAppearanceOverlay">@style/CustomShapeAppearanceBottomSheetDialog</item>
</style>
<style name="CustomShapeAppearanceBottomSheetDialog" parent="">
<item name="cornerFamily">rounded</item>
<item name="cornerSizeTopRight">16dp</item>
<item name="cornerSizeTopLeft">16dp</item>
<item name="cornerSizeBottomRight">0dp</item>
<item name="cornerSizeBottomLeft">0dp</item>
</style>
앱 테마 BottomSheetDialogFragment
를 추가하는 대신 이 메서드를 재정의하는 동일한 동작을 얻을 수 있습니다 bottomSheetDialogTheme
.
@Override public int getTheme() {
return R.style.CustomBottomSheetDialog;
}
이 경우이 themeOverlay BottomSheetDialogFragment
를 모든 앱이 아닌 단일에서만 사용 하고 있습니다.
확장 상태 에 대한 중요 참고 사항 :
펼쳐진 상태에서 BottomSheet 에는 평평한 모서리가 있습니다. github repo 에서 공식 댓글을 확인할 수 있습니다 .
우리 디자인 팀은 둥근 모서리는 스크롤 가능한 콘텐츠를 나타내고 평평한 모서리는 추가 콘텐츠가 없음을 나타냅니다. 따라서 그들은 우리가 fitToContents로이 변경 사항을 추가하는 것을 원하지 않습니다.
이 동작은에서 제공 BottomSheetBehavior
하며 재정의 할 수 없습니다.
그러나 해결 방법이 있습니다-> 면책 : 다음 릴리스에서 작동을 중지 할 수 있습니다 !!
당신은 추가 할 수 있습니다 BottomSheetCallback
에서 BottomSheetDialogFragment
:
@NonNull @Override public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
Dialog dialog = super.onCreateDialog(savedInstanceState);
((BottomSheetDialog)dialog).getBehavior().addBottomSheetCallback(new BottomSheetBehavior.BottomSheetCallback() {
@Override public void onStateChanged(@NonNull View bottomSheet, int newState) {
if (newState == BottomSheetBehavior.STATE_EXPANDED) {
//In the EXPANDED STATE apply a new MaterialShapeDrawable with rounded cornes
MaterialShapeDrawable newMaterialShapeDrawable = createMaterialShapeDrawable(bottomSheet);
ViewCompat.setBackground(bottomSheet, newMaterialShapeDrawable);
}
}
@Override public void onSlide(@NonNull View bottomSheet, float slideOffset) {
}
});
return dialog;
}
@NotNull private MaterialShapeDrawable createMaterialShapeDrawable(@NonNull View bottomSheet) {
ShapeAppearanceModel shapeAppearanceModel =
//Create a ShapeAppearanceModel with the same shapeAppearanceOverlay used in the style
ShapeAppearanceModel.builder(getContext(), 0, R.style.CustomShapeAppearanceBottomSheetDialog)
.build();
//Create a new MaterialShapeDrawable (you can't use the original MaterialShapeDrawable in the BottoSheet)
MaterialShapeDrawable currentMaterialShapeDrawable = (MaterialShapeDrawable) bottomSheet.getBackground();
MaterialShapeDrawable newMaterialShapeDrawable = new MaterialShapeDrawable((shapeAppearanceModel));
//Copy the attributes in the new MaterialShapeDrawable
newMaterialShapeDrawable.initializeElevationOverlay(getContext());
newMaterialShapeDrawable.setFillColor(currentMaterialShapeDrawable.getFillColor());
newMaterialShapeDrawable.setTintList(currentMaterialShapeDrawable.getTintList());
newMaterialShapeDrawable.setElevation(currentMaterialShapeDrawable.getElevation());
newMaterialShapeDrawable.setStrokeWidth(currentMaterialShapeDrawable.getStrokeWidth());
newMaterialShapeDrawable.setStrokeColor(currentMaterialShapeDrawable.getStrokeColor());
return newMaterialShapeDrawable;
}
답변
(가) BottomSheetDialog
모서리가 보이지 않는 이유는 기본 흰색 배경 색상을 설정하는 것입니다, 이것은 당신의 스타일을 재정 의하여 대화의 배경을 투명하게 할 필요가 그들에게 보여주기 위해서입니다 BottomSheetDialog
.
이 스타일을 정의하십시오. res/values/styles/styles.xml
<style name="BottomSheetDialog" parent="Theme.Design.Light.BottomSheetDialog">
<item name="bottomSheetStyle">@style/bottomSheetStyleWrapper</item>
</style>
<style name="bottomSheetStyleWrapper" parent="Widget.Design.BottomSheet.Modal">
<item name="android:background">@android:color/transparent</item>
</style>
이 스타일을 BottomSheetDialog로 설정하십시오.
View view = getLayoutInflater().inflate(R.layout.chooser_bottom_sheet, null);
BottomSheetDialog dialog = new BottomSheetDialog(this,R.style.BottomSheetDialog); // Style here
dialog.setContentView(view);
dialog.show();
답변
rounded_corners_shape라는 모양을 만듭니다.
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners
android:topLeftRadius="8dp"
android:topRightRadius="8dp"/>
<solid android:color="@color/white"/>
</shape>
스타일을 정의하다
<style name="AppBottomSheetDialogTheme"
parent="Theme.Design.Light.BottomSheetDialog">
<item name="bottomSheetStyle">@style/AppModalStyle</item>
</style>
<style name="AppModalStyle" parent="Widget.Design.BottomSheet.Modal">
<item name="android:background">@drawable/rounded_corners_shape</item>
</style>
이 스타일을 사용자 지정 BottomSheetDialogFragment에서 이와 같이 사용하면 작동합니다!
public class CustomDialogFragment extends BottomSheetDialogFragment {
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setStyle(STYLE_NORMAL, R.style. AppBottomSheetDialogTheme);
}
...
}
답변
마지막 버전의 재료 구성 요소 를 사용하는 경우 ShapeAppearance.MaterialComponents.LargeComponent
(하단 시트가이 모양을 사용하므로) 재정의 하고 원하는 값을 다음과 같이 설정하면됩니다.
<style name="ShapeAppearance.YourApp.LargeComponent" parent="ShapeAppearance.MaterialComponents.LargeComponent">
<item name="cornerFamily">rounded</item>
<item name="cornerSize">12dp</item>
</style>
그런 다음 앱 스타일을 설정합니다.
<item name="shapeAppearanceLargeComponent">@style/ShapeAppearance.YourApp.LargeComponent</item>
Gabriele Mariotti의 솔루션 도 비슷하고 작동하지만 이것은 더 간단합니다.
답변
나는 오늘 똑같은 것을 확인하고 있었고 네 코드를 따르는 것이 옳았습니다.
this.getDialog().getWindow().setBackgroundDrawableResource(R.drawable.charge_layout_background);
이것은 조각 배경에 적용되므로 대신 대화 창에서 하단 시트보기를 가져 와서 배경을 변경해야합니다.
@SuppressLint("RestrictedApi")
@Override
public void setupDialog(Dialog dialog, int style) {
super.setupDialog(dialog, style);
View rootView = getActivity().getLayoutInflater().inflate(R.layout.view_member_info,null,false);
unbinder = ButterKnife.bind(this, rootView);
adjustUIComponents();
dialog.setContentView(rootView);
FrameLayout bottomSheet = (FrameLayout) dialog.getWindow().findViewById(android.support.design.R.id.design_bottom_sheet);
bottomSheet.setBackgroundResource(R.drawable.container_background);
}
여기 하단 시트는 변경하려는 실제보기입니다.
답변
의해 답변 코마 깨갱 에서 또 다른 질문 나를 위해 일한, 당신은 그것을 시도해야합니다.
drawable에서 xml을 만듭니다 (예 : dialog_bg.xml).
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android"> <solid android:color="@color/white"/> <corners android:radius="30dp" /> <padding android:left="10dp" android:top="10dp" android:right="10dp" android:bottom="10dp" /> </shape>
이것을 레이아웃 xml 루트 노드에 넣으십시오.
레이아웃 xml의 배경으로 설정하십시오.
android:background="@drawable/dialog_bg"
그리고 onCreateView()
이것을 넣어 :
대화 상자의 배경을 투명하게 설정
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));