自定义的微调器对话框,带有不需要的空间

时间:2011-08-22 21:29:25

标签: android android-layout

我正在创建一个由自定义微调器启动的自定义对话框。我想要做的是自定义微调器调用的对话框。但是,对话框中有一个恼人的空间。我已经尝试了所有资源来修复它,但没有。我也按照question的回答,但没有解决。

在spinner xml文件中,我像这样传递它。它引用了以下名为CustomSpinner的类,它扩展了一个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"/>

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

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

这是我的自定义微调器:

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

}

我的Spinner在Activity中创建如下:

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

以下是对话框的截图:

enter image description here

2 个答案:

答案 0 :(得分:0)

diaPhoto = new Dialog(this, android.R.style.Theme_Translucent_NoTitleBar);

答案 1 :(得分:0)

我按照documentation

解决了这个问题

根据以上文档链接建议的代码:

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