Skip to content

Instantly share code, notes, and snippets.

@tendoasan
Last active May 22, 2019 01:30
Show Gist options
  • Save tendoasan/39c6012bb7e8da88063ce3ade4f64081 to your computer and use it in GitHub Desktop.
Save tendoasan/39c6012bb7e8da88063ce3ade4f64081 to your computer and use it in GitHub Desktop.
[Android-Dialog]Android对话框#Android
/**
* SimpleBottomSheetDialogFragment.newInstance().show(getSupportFragmentManager(), "fragment_simple_bottom_sheet");
*/
public abstract class SimpleBottomSheetDialogFragment extends BottomSheetDialogFragment {
public View rootView;
Unbinder mUnBinder;
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
initEventAndData();
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(getLayoutId(), container);
mUnBinder = ButterKnife.bind(this, view);
return view;
}
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
}
@Override
public void onDestroyView() {
super.onDestroyView();
mUnBinder.unbind();
}
protected void initEventAndData() {
}
@LayoutRes
protected abstract int getLayoutId();
@NonNull
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
Dialog dialog = super.onCreateDialog(savedInstanceState);
// set dialog expanded
dialog.setOnShowListener(new DialogInterface.OnShowListener() {
@Override
public void onShow(DialogInterface dialogInterface) {
BottomSheetDialog bottomSheetDialog = (BottomSheetDialog) dialogInterface;
FrameLayout frameLayout = (FrameLayout) bottomSheetDialog.findViewById(
android.support.design.R.id.design_bottom_sheet);
if (frameLayout != null) {
BottomSheetBehavior.from(frameLayout).setState(BottomSheetBehavior.STATE_EXPANDED);
}
}
});
return dialog;
}
}
public class SimpleDialogFragment extends DialogFragment {
@Override
public void onResume() {
// sizing
int width = getResources().getDimensionPixelSize(R.dimen.popup_width);
int height = getResources().getDimensionPixelSize(R.dimen.popup_height);
Objects.requireNonNull(getDialog().getWindow()).setLayout(width, height);
// set the dialog's width as percentage of the screen
Window window = getDialog().getWindow();
Point size = new Point();
if (window != null){
Display display = window.getWindowManager().getDefaultDisplay();
display.getSize(size);
Dog.e(TAG, "onResume: size: " + size);
window.setLayout((int)(size.x * 0.80), WindowManager.LayoutParams.WRAP_CONTENT);
window.setGravity(Gravity.CENTER);
}
// set full-screen dialog
Window window = getDialog().getWindow();
if (window != null){
LayoutParams params = window.getAttributes();
params.width = LayoutParams.MATCH_PARENT;
params.height = LayoutParams.MATCH_PARENT;
window.setAttributes(params);
}
super.onResume();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment