Android 带有自定义视图alwas的DialogFragment采用全屏宽度
我使用Android 带有自定义视图alwas的DialogFragment采用全屏宽度,android,Android,我使用DialogFragment和从XML填充的自定义View以及AlertDialog.Builder中的标准按钮。我的onCreateDialog如下所示: @Override public Dialog onCreateDialog(Bundle savedInstanceState) { AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); LayoutInflater inflate
DialogFragment
和从XML填充的自定义View
以及AlertDialog.Builder
中的标准按钮。我的onCreateDialog
如下所示:
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
LayoutInflater inflater = getActivity().getLayoutInflater();
View rootView = inflater.inflate(R.layout.toolkit_picker_color, null);
setupView(rootView);
builder.setView(rootView);
builder.setPositiveButton(android.R.string.ok, this);
builder.setNegativeButton(android.R.string.cancel, this);
return builder.create();
}
使用自定义视图的布局:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<LinearLayout
android:id="@id/toolkit.container.a"
android:layout_width="100dp"
android:layout_height="52dp"
android:layout_alignParentTop="true"
android:gravity="center" >
<include layout="@layout/toolkit_picker_color_modes" />
</LinearLayout>
</RelativeLayout>
显示对话框时,其高度(如预期)将包裹自定义布局的内容。但是,对话框的宽度占据整个屏幕,就好像使用了match\u parent
我从源代码中看到过基于设置对话框宽度的解决方案,但这不是我想要的,而且它不适用于WRAP\u CONTENT
如果不使用AlertDialog.Builder
,则使用DialogFragment
不会出现问题。但是,我需要这些按钮
有没有一种方法可以根据其自定义的
视图将对话框片段的宽度设置为使用AlertDialog.Builder
包装内容?我不确定包装内容,android:layout\u alignParentTop=“true”
和android:gravity=“center”
是如何交互的。你能试着省去其中的一些并迭代所有的组合吗?@mcwise我试着根本不使用AlertDialog.Builder.setView
,只留下按钮,甚至不留下按钮,只使用AlertDialog.Builder.setMessage
——同样的结果。因此,我认为这不是布局本身的问题。