我需要一张底页并没有占据平板电脑的所有宽度。但它忽略了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;
}
}
答案 0 :(得分:2)
我不确定这是否是最佳解决方案,但它至少对我有用。
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);
}
}
}
P.S。我尝试在onStart()
方法中设置对话框宽度,但它不起作用,我不知道为什么。