4

如您所见,在我的 ListView 的底部列表元素下方,我似乎无法摆脱多余的空间。我尝试过Relative 和Linearlayout,两者看起来都像这样。这是代码: 在此处输入图像描述

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

2 回答 2

3

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

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

新代码如下所示:

    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();
于 2012-06-05T20:49:05.453 回答
2

如果您在只有 ListView 的警报对话框(通过 setView())上设置自定义视图,那么您不需要使用自定义视图。如果调用设置适配器,构建器将自动将 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();
}
于 2012-08-07T12:04:46.013 回答