6

我需要一张底片没有占据平板电脑的所有宽度。但它忽略layout_width了 xml 中的属性。我怎样才能做到?我的底片类:

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;
    }
}
4

1 回答 1

5

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

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);
        }
    }
}

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

于 2016-07-10T11:05:12.480 回答