定制微调对话与不需要的空间不需要、空间

2023-09-04 05:49:19 作者:不要挽留不要回头

我要创建由一个自定义的微调开始的自定义对话框。我试图做的是自定义对话框微调电话。然而,在该对话框中的恼人的空间。我tryied我所有的资源来解决这个问题,但没有什么。我也跟着这个question's回答,但并没有解决。

I'm creating a custom Dialog that is started by a custom spinner. What I was trying to do is customize the dialog the spinner calls. However, there is an annoying space in the dialog. I've tryied all my resources to fix it, but nothing. I also followed this question's answer but didn't solve.

在微调XML文件我将它传递这样。它引用名为CustomSpinner以下类,扩展一个微调:

In the spinner xml file I pass it like this. It references the following class named CustomSpinner, that extends a Spinner:

 <com.myproject.CustomSpinner
    android:id="@+id/customSpinner"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:paddingLeft="13dp"
    android:prompt="@string/my_spinner"/>

和我有这个类,它是我的自定义对话框类:

And I have this class that is my custom dialog class:

public class CustomSpinnerDialog extends Dialog implements OnItemClickListener, View.OnClickListener
{
    private OnItemSelectedListener onItemSelectedListener;

    public DialogInterface.OnClickListener mListener;
    public Context mContext;    

    public interface OnItemSelectedListener
    {
        public void onItemSelected(String itemValue);       
    }

    public CustomSpinnerDialog(Context context, CustomSpinner.SpinnerAdapter spinnerAdapter, DialogInterface.OnClickListener listener) 
    {
        super(context);     
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        this.setContentView(R.layout.custom_spinner);
        mListener = listener;
        mContext = context;

        ListView listView = (ListView) this.findViewById(R.id.listview);
        listView.setAdapter(spinnerAdapter);
        listView.setOnItemClickListener(this);
        listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);        
    }

    public void setOnItemSelectedListener(OnItemSelectedListener listener)
    {
        this.onItemSelectedListener = listener;
    }

    @Override
    public void onClick(View v) 
    {
        if(mListener != null)
            mListener.onClick(this, DialogInterface.BUTTON_POSITIVE);           
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) 
    {
        if(mListener != null)
            mListener.onClick(this, position);
        String text = (String) parent.getItemAtPosition(position);
        onItemSelectedListener.onItemSelected(text);        
    }

    public void setDialogTitle(String title)
    {
        TextView titleText = (TextView) this.findViewById(R.id.titleText);
        titleText.setText(title);
    }   
}

这是我的自定义微调:

And this is my custom spinner:

public class CustomSpinner extends Spinner implements DialogInterface.OnClickListener
{
    public Context mContext;
    public String[] mDataList;

    public CustomSpinner(Context context, AttributeSet attrs) 
    {
        super(context, attrs);  
        this.mContext = context;
    }

    @Override
    public boolean performClick() 
    {   
        boolean handled = false;
        if (!handled) 
        {
            handled = true;                                                

            CustomSpinnerDialog dialog = new CustomSpinnerDialog(mContext, (ListAdapter) getAdapter(), this, R.style.FullHeightDialog);               
            dialog.setDialogTitle(mContext.getResources().getString((R.string.my_dialog_text)));                              
            dialog.show();
        }        
        return handled;
    }

     @Override
     public void onClick(DialogInterface dialog, int which) 
     {
         setSelection(which);        
         dialog.dismiss();
     }

}

我的微调是这样创造了一个活动:

My Spinner is created like this in an Activity:

cSpinner= (CustomSpinner) findViewById(R.id.customSpinner);     
cSpinner.setDataList(dataList);
cSpinner.setTag("CustomSpinner");
cSpinner.setOnItemSelectedListener(controller);

下面是如何对话框如下截图:

Here is a screenshot of how the dialog looks like:

推荐答案

我解决它遵循的文档

和设在该code作为建议由上面的文档链接:

And based in this code as advised by the documentation link above:

AlertDialog.Builder builder;
AlertDialog alertDialog;

Context mContext = getApplicationContext();
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.custom_dialog,
                               (ViewGroup) findViewById(R.id.layout_root));

TextView text = (TextView) layout.findViewById(R.id.text);
text.setText("Hello, this is a custom dialog!");
ImageView image = (ImageView) layout.findViewById(R.id.image);
image.setImageResource(R.drawable.android);

builder = new AlertDialog.Builder(mContext);
builder.setView(layout);
alertDialog = builder.create();