开发者

Customized Spinner Dialog with unwanted space

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.

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开发者_如何学JAVA) findViewById(R.id.customSpinner);     
cSpinner.setDataList(dataList);
cSpinner.setTag("CustomSpinner");
cSpinner.setOnItemSelectedListener(controller);

Here is a screenshot of how the dialog looks like:

Customized Spinner Dialog with unwanted space


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


I solved it following the documentation

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜