Jia*_*Guo 12 android android-dialogfragment bottom-sheet
我最近使用了android.support.design.widget.BottomSheetDialogFragment.我想做一些类似于谷歌联系人应用程序的东西,它的BottomSheet可以覆盖工具栏和状态栏.但是,当我使用BottomSheetDialogFragment实现它时,结果如下:

如您所见,活动的工具栏仍然可见.这是我的代码BottomSheetDialogFragment:
public class KeyDetailFragment extends BottomSheetDialogFragment {
private BottomSheetBehavior.BottomSheetCallback mBottomSheetBehaviorCallback = new BottomSheetBehavior.BottomSheetCallback() {
@Override
public void onStateChanged(@NonNull View bottomSheet, int newState) {
if (newState == BottomSheetBehavior.STATE_HIDDEN) {
dismiss();
}
}
@Override
public void onSlide(@NonNull View bottomSheet, float slideOffset) {
}
};
@Override
public void setupDialog(Dialog dialog, int style) {
super.setupDialog(dialog, style);
View contentView = View.inflate(getActivity(), R.layout.sheet_key, null);
dialog.setContentView(contentView);
View parent = (View) contentView.getParent();
parent.setFitsSystemWindows(true);
BottomSheetBehavior bottomSheetBehavior = BottomSheetBehavior.from(parent);
contentView.measure(0, 0);
bottomSheetBehavior.setPeekHeight(contentView.getMeasuredHeight());
CoordinatorLayout.LayoutParams params = (CoordinatorLayout.LayoutParams) parent.getLayoutParams();
if (params.getBehavior() instanceof BottomSheetBehavior) {
((BottomSheetBehavior)params.getBehavior()).setBottomSheetCallback(mBottomSheetBehaviorCallback);
}
params.gravity = Gravity.TOP | Gravity.CENTER_HORIZONTAL;
parent.setLayoutParams(params);
}
}
Run Code Online (Sandbox Code Playgroud)
我提到了源代码,我发现一个属性让我感兴趣:
private static int getThemeResId(Context context, int themeId) {
if (themeId == 0) {
// If the provided theme is 0, then retrieve the dialogTheme from our theme
TypedValue outValue = new TypedValue();
if (context.getTheme().resolveAttribute(
R.attr.bottomSheetDialogTheme, outValue, true)) {
themeId = outValue.resourceId;
} else {
// bottomSheetDialogTheme is not provided; we default to our light theme
themeId = R.style.Theme_Design_Light_BottomSheetDialog;
}
}
return themeId;
}
Run Code Online (Sandbox Code Playgroud)
bottomSheetDialogTheme这里的属性可能会改变底层的样式,但我不知道如何改变它,我怀疑这是否有效.有人可以给我解决方案,它可以覆盖工具栏和状态栏吗?
小智 20
试试这个.这个对我有用.
@Override
public void setupDialog(Dialog dialog, int style) {
super.setupDialog(dialog, style);
View inflatedView = View.inflate(getContext(), R.layout.fragment_coupon, null);
dialog.setContentView(inflatedView);
CoordinatorLayout.LayoutParams params = (CoordinatorLayout.LayoutParams) ((View) inflatedView.getParent()).getLayoutParams();
CoordinatorLayout.Behavior behavior = params.getBehavior();
if (behavior != null && behavior instanceof BottomSheetBehavior) {
((BottomSheetBehavior) behavior).setBottomSheetCallback(mBottomSheetBehaviorCallback);
}
View parent = (View) inflatedView.getParent();
parent.setFitsSystemWindows(true);
BottomSheetBehavior bottomSheetBehavior = BottomSheetBehavior.from(parent);
inflatedView.measure(0, 0);
DisplayMetrics displaymetrics = new DisplayMetrics(); getActivity().getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
int screenHeight = displaymetrics.heightPixels;
bottomSheetBehavior.setPeekHeight(screenHeight);
if (params.getBehavior() instanceof BottomSheetBehavior) {
((BottomSheetBehavior)params.getBehavior()).setBottomSheetCallback(mBottomSheetBehaviorCallback);
}
params.height = screenHeight;
parent.setLayoutParams(params);
}
Run Code Online (Sandbox Code Playgroud)
无法找到这个问题的解决方案,但可以建议一个替代方案来帮助我达到相同的目的。这是参考: http://www.hidroh.com/2016/06/17/bottom-sheet-everything/
本文介绍了创建底部工作表活动并为其添加背景阴影。
| 归档时间: |
|
| 查看次数: |
21912 次 |
| 最近记录: |