Android ios-like pop-up box
Preparation:
Public class ActionSheet {
Public interface OnActionSheetSelected {
Void onClick (int whichButton );
}
Private ActionSheet (){
}
Public static Dialog showSheet (final Context context, final int layoutId,
Final OnActionSheetSelected actionSheetSelected,
Final OnCancelListener cancelListener ){
Final Dialog dialog = new Dialog (context, R. style. ActionSheet );
Final LayoutInflater inflater = (LayoutInflater) context
. GetSystemService (Context. LAYOUT_INFLATER_SERVICE );
Final LinearLayout layout = (LinearLayout) inflater. inflate (layoutId,
Null );
Final int cFullFillWidth = 10000;
Layout. setMinimumWidth (cFullFillWidth );
Final Window w = dialog. getWindow ();
Final WindowManager. LayoutParams lp = w. getAttributes ();
Lp. x = 0;
Final int cMakeBottom =-1000;
Lp. y = cMakeBottom;
Lp. gravity = Gravity. BOTTOM;
Dialog. onalog wattributeschanged (lp );
Dialog. setCanceledOnTouchOutside (true );
If (cancelListener! = Null ){
Dialog. setOnCancelListener (cancelListener );
}
Dialog. setContentView (layout );
Dialog. show ();
Return dialog;
}
}
<Style name = "ActionSheet" parent = "@ android: style/Theme. Dialog">
Application:
Private ActionSheet. OnActionSheetSelected mOnActionSheetSelected;
private Dialog mActionSheet;
Call the showGetPhotoDialog () method:
public void onActionSheetClicked(final View view) { mOnActionSheetSelected.onClick(view.getId());}public void setOnSheetClicked(final ActionSheet.OnActionSheetSelected onActionSheetSelected) { mOnActionSheetSelected = onActionSheetSelected;}private void showGetPhotoDialog() { mActionSheet = ActionSheet.showSheet(this, R.layout.actionsheet_dialog, getOnActionSheetClicked(), null);}private ActionSheet.OnActionSheetSelected getOnActionSheetClicked() { return new ActionSheet.OnActionSheetSelected() { @Override public void onClick(final int whichButton) { switch (whichButton) { case R.id.tv_button1: break;
case R.id.tv_button2: break;
Case R. id.TV _calendar:
MActionSheet. dismiss ();
Break;
Default:
Break;
}
MActionSheet. dismiss ();}
};
}
Layout:
<? Xml version = "1.0" encoding = "UTF-8"?>
<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Android: layout_width = "match_parent"
Android: layout_height = "wrap_content"
Android: background = "@ color/clear"
Android: orientation = "vertical"
Android: padding = "5dp">
<TextView
Android: id = "@ + id/TV _button1"
Android: layout_width = "match_parent"
Android: layout_height = "wrap_content"
Android: background = "@ drawable/actionsheet_top_selector"
Android: clickable = "true"
Android: gravity = "center"
Android: onClick = "onActionSheetClicked"
Android: text = "button 1"
Android: textSize = "18sp"/>
<TextView
Android: id = "@ + id/TV _button2"
Android: layout_width = "match_parent"
Android: layout_height = "wrap_content"
Android: background = "@ drawable/actionsheet_bottom_selector"
Android: clickable = "true"
Android: gravity = "center"
Android: onClick = "onActionSheetClicked"
Android: text = "button 2"
Android: textColor = "@ color/green_title_bar"
Android: textSize = "18sp"/>
<TextView
Android: id = "@ + id/cancel"
Android: layout_width = "match_parent"
Android: layout_height = "wrap_content"
Android: layout_marginTop = "10dp"
Android: background = "@ drawable/actionsheet_single_selector"
Android: clickable = "true"
Android: gravity = "center"
Android: onClick = "onActionSheetClicked"
Android: text = "@ string/cancle"
Android: textColor = "@ color/cancle_blue"
Android: textSize = "18sp"/>
</LinearLayout>