Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/206.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 设置对话框片段的大小_Android_Android Dialogfragment - Fatal编程技术网

Android 设置对话框片段的大小

Android 设置对话框片段的大小,android,android-dialogfragment,Android,Android Dialogfragment,我一直在尝试许多命令来设置我的对话框片段的大小。它只包含一个颜色选择器,因此我删除了对话框的背景和标题: getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE); getDialog().getWindow().setBackgroundDrawable( new ColorDrawable(android.graphics.Color.TRANSPARENT)); 但是,我也希望将对话框放置在我想要的位置,这是有

我一直在尝试许多命令来设置我的
对话框片段的大小。它只包含一个颜色选择器,因此我删除了对话框的背景和标题:

getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE);
getDialog().getWindow().setBackgroundDrawable(
    new ColorDrawable(android.graphics.Color.TRANSPARENT));
但是,我也希望将对话框放置在我想要的位置,这是有问题的。我使用:

WindowManager.LayoutParams params = getDialog().getWindow().getAttributes();
params.width = LayoutParams.WRAP_CONTENT;
params.height =  LayoutParams.WRAP_CONTENT;
params.gravity = Gravity.LEFT;
getDialog().getWindow().setAttributes(params);
但仍然存在一个(大)障碍:即使我的对话框窗格不可见,它仍然有一定的大小,并且限制了对话框的位置。这里的
LayoutParams.WRAP_CONTENT
将此窗格的大小限制为“我的颜色选择器”,但由于某些原因,它不起作用


有人能做类似的事情吗?

经过反复试验,我找到了解决办法

下面是我的DialogFragment类的实现:

public class ColorDialogFragment extends SherlockDialogFragment {

    public ColorDialogFragment() {
    //You need to provide a default constructor
    }

    @Override
    public View onCreateView(LayoutInflater inflater, 
                     ViewGroup container,
                     Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.dialog_color_picker, container);
    // R.layout.dialog_color_picker is the custom layout of my dialog
    WindowManager.LayoutParams wmlp = getDialog().getWindow().getAttributes();
    wmlp.gravity = Gravity.LEFT;
    return view;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setStyle(DialogFragment.STYLE_NO_FRAME, R.style.colorPickerStyle);
    // this setStyle is VERY important.
    // STYLE_NO_FRAME means that I will provide my own layout and style for the whole dialog
    // so for example the size of the default dialog will not get in my way
    // the style extends the default one. see bellow.        
    }

  }
R.style.colorPickerStyle对应于:

<style name="colorPickerStyle" parent="Theme.Sherlock.Light.Dialog">
    <item name="android:backgroundDimEnabled">false</item>
    <item name="android:cacheColorHint">@android:color/transparent</item>
    <item name="android:windowBackground">@android:color/transparent</item>
</style>

我遇到了一个类似的问题,即不能在代码中设置dialogFragment的宽度和高度,经过多次尝试,我找到了一个解决方案

以下是自定义DialogFragment的步骤:

// SoundPicker.java
// extends DialogFragment
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    builder.setTitle(getActivity().getString(R.string.txt_sound_picker_dialog_title));

    LayoutInflater layoutInflater = LayoutInflater.from(getActivity());
    View view = layoutInflater.inflate(R.layout.fragment_sound_picker, null);
    RecyclerView rv = (RecyclerView) view.findViewById(R.id.rv_sound_list);
    rv.setLayoutManager(new LinearLayoutManager(getActivity()));

    SoundPickerAdapter soundPickerAdapter = new SoundPickerAdapter(getActivity().getApplicationContext(), this, selectedSound);
    List<SoundItem> items = getArguments().getParcelableArrayList(SOUND_ITEMS);
    soundPickerAdapter.setSoundItems(items);
    soundPickerAdapter.setRecyclerView(rv);
    rv.setAdapter(soundPickerAdapter);

    // Here's the LayoutParams setup
    ViewGroup.LayoutParams layoutParams = rv.getLayoutParams();
    layoutParams.width = RelativeLayout.LayoutParams.MATCH_PARENT;
    layoutParams.height = getListItemHeight() * (items.size() + 1);
    view.setLayoutParams(layoutParams);

    builder.setView(view);
    builder.setCancelable(true);

    builder.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {
        // ...
    });

    builder.setPositiveButton(R.string.txt_ok, new DialogInterface.OnClickListener() {
        // ...
    });

    return builder.create();
}

@Override
public void onResume() {
    Window window = getDialog().getWindow();
    window.setLayout(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
    super.onResume();
}

private int getListItemHeight() {
    TypedValue typedValue = new TypedValue();
    getActivity().getTheme().resolveAttribute(R.attr.listPreferredItemHeight, typedValue, true);
    DisplayMetrics metrics = new android.util.DisplayMetrics(); getActivity().getWindowManager().getDefaultDisplay().getMetrics(metrics);
    return (int) typedValue.getDimension(metrics);
}
1.在方法上从xml膨胀自定义视图

public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState)
{

    getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE);
    getDialog().setCanceledOnTouchOutside(true);

    View view = inflater.inflate(R.layout.XXX,
            container, false);
    //TODO:findViewById, etc
    return view;
}
2.在onResume()中设置对话框的宽度和高度,在onResume()/onStart()中设置rember,在其他方法中似乎不起作用

public void onResume()
{
    super.onResume();
    Window window = getDialog().getWindow();
    window.setLayout(width, height);
    window.setGravity(Gravity.CENTER);
    //TODO:
}  

对于我的用例,我希望DialogFragment与项目列表的大小相匹配。片段视图是一个名为
fragment\u sound\u picker
的布局中的回收视图。我在RecyclerView周围添加了一个包装器RelativeLayout

我已经用
R.attr.listItemPreferredHeight
在名为
item\u sound\u choice
的布局中设置了单个列表项视图的高度

DialogFragment从膨胀视图的RecyclerView获取LayoutParams实例,将LayoutParams高度调整为列表长度的倍数,并将修改后的LayoutParams应用于膨胀父视图

结果是DialogFragment完美地包装了简短的选择列表。它包括窗口标题和取消/确定按钮

以下是DialogFragment中的设置:

// SoundPicker.java
// extends DialogFragment
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    builder.setTitle(getActivity().getString(R.string.txt_sound_picker_dialog_title));

    LayoutInflater layoutInflater = LayoutInflater.from(getActivity());
    View view = layoutInflater.inflate(R.layout.fragment_sound_picker, null);
    RecyclerView rv = (RecyclerView) view.findViewById(R.id.rv_sound_list);
    rv.setLayoutManager(new LinearLayoutManager(getActivity()));

    SoundPickerAdapter soundPickerAdapter = new SoundPickerAdapter(getActivity().getApplicationContext(), this, selectedSound);
    List<SoundItem> items = getArguments().getParcelableArrayList(SOUND_ITEMS);
    soundPickerAdapter.setSoundItems(items);
    soundPickerAdapter.setRecyclerView(rv);
    rv.setAdapter(soundPickerAdapter);

    // Here's the LayoutParams setup
    ViewGroup.LayoutParams layoutParams = rv.getLayoutParams();
    layoutParams.width = RelativeLayout.LayoutParams.MATCH_PARENT;
    layoutParams.height = getListItemHeight() * (items.size() + 1);
    view.setLayoutParams(layoutParams);

    builder.setView(view);
    builder.setCancelable(true);

    builder.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {
        // ...
    });

    builder.setPositiveButton(R.string.txt_ok, new DialogInterface.OnClickListener() {
        // ...
    });

    return builder.create();
}

@Override
public void onResume() {
    Window window = getDialog().getWindow();
    window.setLayout(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
    super.onResume();
}

private int getListItemHeight() {
    TypedValue typedValue = new TypedValue();
    getActivity().getTheme().resolveAttribute(R.attr.listPreferredItemHeight, typedValue, true);
    DisplayMetrics metrics = new android.util.DisplayMetrics(); getActivity().getWindowManager().getDefaultDisplay().getMetrics(metrics);
    return (int) typedValue.getDimension(metrics);
}

使用此代码调整对话框片段的大小

public void onResume() {
        super.onResume();
        Window window = getDialog().getWindow();
        window.setLayout(250, 100);
        window.setGravity(Gravity.RIGHT);
    }

对话框的大小和位置需要在对话框构造函数中设置。您必须重写DialogFragment的onCreateDialog,并在其中创建自定义对话框。+Sudar Nimalan谢谢,这是解决方案的一部分。对于未来的读者,相关问题:1)2)非常感谢!我想问一下,您对将对话框放置在您想要的位置做了什么?您可以这样做吗?如何操作?在onCreateView中,您还可以通过以下命令访问布局的参数:WindowManager.LayoutParams wmlp=getDialog().getWindow().getAttributes()。例如,如果要将对话框放在屏幕左侧,只需使用wmlp.gravity=gravity.left;然后wmlp.x=this.getResources().getDimensionPixelOffset(R.dimen.dialog_position);。。。除了如何设置对话框的宽度之外?这对我也很有用。出于某种原因,我的上/下页边距被忽略了,但我将其改为填充,这对我很有效。
onResume()
中的代码也可以在
onCreateDialog()
中工作,适用于那些已经将该方法用于各种其他对话框黑客的人。效果非常好。它不需要onCreateView中的任何自定义项。。只是onResume()中的窗口内容很好。真是天才。继续努力。出于好奇,我想知道为什么这个问题会发生在少数设备上&而不是其他设备上?我认为宽度和高度取决于屏幕大小,但我无法真正找到模式,因为我在将大小设置为dialogfragment时遇到了问题。如果我把高度设为150,如果得到碎片的高度,它会给我其他的答案。请帮我解决这个问题,我认为宽度和高度取决于屏幕大小,但我无法真正找到图案,因为我在将大小设置为dialogfragment时遇到了问题。如果我把高度设为150,如果得到碎片的高度,它会给我其他的答案。请帮我回答这个问题
public void onResume() {
        super.onResume();
        Window window = getDialog().getWindow();
        window.setLayout(250, 100);
        window.setGravity(Gravity.RIGHT);
    }