DialogFragment与ListView之间的额外空间 [英] DialogFragment with extra space below ListView

查看:133
本文介绍了DialogFragment与ListView之间的额外空间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您可以看到,在ListView中的底部列表元素下方,有多余的空间我似乎无法摆脱。我已经尝试过Relative和Linearlayout,都是这样的。这里是代码:

  public class ChooseDialog extends DialogFragment implements 
DialogInterface.OnClickListener {

String URLhome;
字符串标题;
字符串类型;

/ * public static ChooseDialog newInstance(){
ChooseDialog dialog = new ChooseDialog();
Log.v(a,shit running);
Bundle bundle = new Bundle();
dialog.setArguments(bundle);
返回对话框;
} * /

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(取消,这个);
LayoutInflater inflater =(LayoutInflater)getActivity()。getSystemService(Context.LAYOUT_INFLATER_SERVICE);
查看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(touc ...,items [which] .toString());

}}
);


return builder.create();

}

@Override
public void onClick(DialogInterface dialog,int which){
// TODO自动生成的方法存根

}

}

启动对话框的代码:

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

private void showNationalityDialog(){
FragmentManager fm = getSupportFragmentManager();
ChooseDialog nationalityDialog = new ChooseDialog(国籍);

nationalityDialog.show(fm,fragment_edit_name);
}


解决方案

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



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



新代码如下所示:

 开关(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(取消,这个);
builder.setView(dialogLayout);

return builder.create();


As you can see, below the bottom list element in my ListView, there is excess space I can't seem to be rid of. I've tried Relative and Linearlayout, both look like this. Here's the code:

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

    }

}

And the code that launches the dialog:

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

解决方案

I know this question never drew much attention, but I finally solved the problem.

By using the listview that I created in XML rather than setting the builder's adapter, I managed to get rid of all the excess space.

Here's what the new code looks like:

    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();

这篇关于DialogFragment与ListView之间的额外空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆