DialogFragment在ListView下面有额外的空间

时间:2012-05-25 21:46:40

标签: android dialog android-fragments

正如你所看到的,在我的ListView中的底部列表元素下面,有一些我似乎无法摆脱的空间。我尝试过Relative和Linearlayout,看起来都像这样。这是代码: enter image description here

public class ChooseDialog extends DialogFragment implements
        DialogInterface.OnClickListener {

    String URLhome;
    String Title;
    String type;

/*  public static ChooseDialog newInstance() {
        ChooseDialog dialog = new ChooseDialog();
        Log.v("a", "shit runs");
        Bundle bundle = new Bundle();
        dialog.setArguments(bundle);
        return dialog;
    }*/

    public ChooseDialog(String type) {
        this.type = type;
    }

    @Override
    public void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        setCancelable(true);
        int style = DialogFragment.STYLE_NORMAL, theme = 0;
        setStyle(style, theme);
    }

     @Override
        public Dialog onCreateDialog(Bundle savedInstanceState) {
            AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
            builder.setTitle(type);
            builder.setNegativeButton("Cancel", this);
            LayoutInflater inflater = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            View dialogLayout = inflater.inflate(R.layout.dialog, null);
            builder.setView(dialogLayout);

            final String[] items = {"Red", "Green", "Blue" };

            builder.setAdapter(new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, items), 
                    new DialogInterface.OnClickListener() {


                public void onClick(DialogInterface dialog, int which) {
                    Log.v("touched: ", items[which].toString());

                }} 
                );


            return builder.create();

        }

    @Override
    public void onClick(DialogInterface dialog, int which) {
        // TODO Auto-generated method stub

    }

}

启动对话框的代码:

public OnClickListener listener = new OnClickListener() {
    public void onClick(View v) {
        showNationalityDialog();
    }
};

private void showNationalityDialog() {
    FragmentManager fm = getSupportFragmentManager();
    ChooseDialog nationalityDialog = new ChooseDialog("Nationality");

    nationalityDialog.show(fm, "fragment_edit_name");
}

2 个答案:

答案 0 :(得分:3)

我知道这个问题从未引起太多关注,但我终于解决了这个问题。

通过使用我在XML中创建的listview而不是设置构建器的适配器,我设法摆脱了所有多余的空间。

以下是新代码的样子:

    switch (editText.getId()) {
    case (0) :
    ListView list = (ListView) dialogLayout.findViewById(R.id.listView1);
    list.setAdapter(new ArrayAdapter<String>(activity, R.layout.dialoglist, 
            activity.getResources().getStringArray(R.array.ageArray)));
    list.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                long arg3) {
            editText.setText(activity.getResources().getStringArray(R.array.ageArray)[arg2]);
            dismiss();
        }   
    });
    builder = (Integer.parseInt(android.os.Build.VERSION.SDK) < 11)? new AlertDialog.Builder(activity) : 
        new AlertDialog.Builder(activity, android.R.style.Theme_Translucent);
    builder.setNegativeButton("Cancel", this);
    builder.setView(dialogLayout);

    return builder.create();

答案 1 :(得分:2)

如果要在警告对话框上设置自定义视图(通过setView()),只有ListView,则不需要使用自定义视图。如果调用set adapter,构建器将自动将ListView添加到视图中。 列表视图末尾的额外空间可能是您的自定义视图,没有内容。

例如:

    @Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    final AccountChooserListAdapter adapter = new AccountChooserListAdapter(getActivity(), R.layout.choose_account_list_item,
            accountMetadataFactory.getAccountsAsList());

    return new AlertDialog.Builder(getActivity())
            .setCancelable(true)
            .setTitle(getActivity().getString(R.string.title_add_account))
            .setAdapter(adapter, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    listener.onAddAccount(which);
                }
            })
            .setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.dismiss();
                }
            })
            .create();
}