关键在于设置Style
调用
<style name="Dialog_Fullscreen">
<item name="android:windowBackground">@android:color/transparent</item>
<item name="android:windowIsFloating">false</item>
<item name="android:windowFullscreen">false</item>
<item name="android:windowNoTitle">true</item>
<item name="android:backgroundDimEnabled">true</item>
</style>
public class SpinnerDialog extends Dialog {
private Context mContext;
private ArrayAdapter<CharSequence> mAdapter = null;
private OnConfirmListener mListener;
private View mRoot;
public SpinnerDialog(Context context) {
this(context, R.style.Dialog_Fullscreen);
}
public SpinnerDialog(Context context, int theme) {
super(context, R.style.Dialog_Fullscreen);
mContext = context;
}
public void setSpinnerData(int resId) {
mAdapter = ArrayAdapter.createFromResource(mContext, resId,
android.R.layout.simple_spinner_item);
}
public void setSpinnerData(ArrayAdapter<CharSequence> adapter) {
this.mAdapter = adapter;
}
private View getView() {
mAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// get layout
View v = LayoutInflater.from(mContext).inflate(R.layout.spinner_dialog,
null);
// bind data
final Spinner spinner = (Spinner) v.findViewById(R.id.item_selector);
spinner.setAdapter(mAdapter);
// set click event
v.findViewById(R.id.btnOK).setOnClickListener(
new View.OnClickListener() {
public void onClick(View v) {
if (mListener != null) {
int idx = spinner.getSelectedItemPosition();
String val = spinner.getSelectedItem().toString();
mListener.onConfirm(idx, val);
}
dismiss();
}
});
v.findViewById(R.id.btnCancel).setOnClickListener(
new View.OnClickListener() {
public void onClick(View v) {
cancel();
}
});
return v;
}
public void setOnConfirmListener(OnConfirmListener listener) {
mListener = listener;
}
@Override
protected void onStart() {
super.onStart();
if (mRoot == null) {
mRoot = getView();
setContentView(mRoot);
}
}
/**
* Interface definition for a callback to be invoked when press OK button.
*/
public interface OnConfirmListener {
public void onConfirm(int idx, String value);
}
}
调用
SpinnerDialog dlg = new SpinnerDialog(this);
dlg.setSpinnerData(R.array.xxx);
dlg.setOnConfirmListener(listener);