android - 如何控制 Bottom Sheet 宽度?

标签 android bottom-sheet

我需要一个 Bottom Sheet 不占用平板电脑上的所有宽度。但它忽略了 xml 中的 layout_width 属性。我怎样才能做到? 我的底层表类:

public class DetailsFragment extends BottomSheetDialogFragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        //Create fragment root view
        View view = inflater.inflate(R.layout.fragment_details, container, false);
        //Set toolbar
        Toolbar toolbar = (Toolbar) view.findViewById(R.id.toolbar);
        Drawable icClose = VectorDrawable.getDrawable(getContext(), R.drawable.ic_close_white_24dp);
        toolbar.setNavigationIcon(icClose);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
        toolbar.inflateMenu(R.menu.details);
        RecyclerView rvContent = (RecyclerView) view.findViewById(R.id.rvContent);
        FullExpandedListManager lm = new FullExpandedListManager(getContext());
        rvContent.setLayoutManager(lm);
        rvContent.setHasFixedSize(true);
        rvContent.setAdapter(mAdapter);

        return view;
    }
}

最佳答案

我不确定这是否是最佳解决方案,但至少对我有用。

public class DetailsFragment extends BottomSheetDialogFragment {
    @NonNull
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        return new BottomSheetDialog(getContext(), getTheme());
    }

    public static class BottomSheetDialog extends android.support.design.widget.BottomSheetDialog {

        public BottomSheetDialog(@NonNull Context context) {
            super(context);
        }

        protected BottomSheetDialog(@NonNull Context context, final boolean cancelable,
                                    OnCancelListener cancelListener) {
            super(context, cancelable, cancelListener);
        }

        public BottomSheetDialog(@NonNull Context context, @StyleRes final int theme) {
            super(context, theme);
        }

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            getWindow().setLayout(300 /*our width*/, ViewGroup.LayoutParams.MATCH_PARENT);
        }
    }
}

附言我尝试在 onStart() 方法中设置对话框宽度,但它不起作用,我也不知道为什么。

关于android - 如何控制 Bottom Sheet 宽度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38290899/

相关文章:

java - Android LibGDX 2d 在碰撞、崩溃时删除项目

Android Spinner 下拉 View 不透明

java - 如何获取当前位置和下一个当前位置之间的距离

android - 我正在尝试编译我的 jnicode,它显示错误 _JNIEnv::GetShortArrayElements(JNIEnv*&, _jshortArray*&, int)'

android - 从对话框更新 RecyclerView

android - BottomSheet + ViewPager2 拖动隐藏不起作用

java - 在应用程序中实现 BottomSheet

android - Blogger API 检索在 WebView 中加载内容的特定帖子问题

flutter - TabBarView 中的模态 Bottom Sheet 被键盘隐藏

android - 限制 Bottom Sheet 单向下滑动