How to make a dialog slide from bottom to middle of screen in android

后端 未结 8 2527
别跟我提以往
别跟我提以往 2020-11-30 17:10

I want to show a dialog on my activity with animation. My dialog will slide from bottom of activity to middle of activity.

/****Edit****/

I\'m sorry for my q

8条回答
  •  半阙折子戏
    2020-11-30 17:55

    you can use bottom Sheets. I put some infromation about it.

    With Android Support Library 23.2, Google announced support for Bottom Sheets. According to Material Design, Bottom Sheets are elements displayed only as a result of a user-initiated action, used to reveal more content.

    There are two major types of bottom sheets:

    Modal bottom sheets are alternatives to menus or simple dialogs. They can also present deep-linked content from other apps. They are primarily for mobile.

    Persistent bottom sheets present in-app content

    There is simple example

    Making a BottomSheet on Android is quite easy, you just have to use a CoordinatorLayout as main element of your layout and attach a BottomSheet behavior to a view.

    1 step - activity_main.xml

    
    
    
    
    
        
    
    
    
    

    2 step - add bottom_sheet.xml

    
    
    
    
    
        
    
    
    

    3 step - make your MainActivity like this:

    public class MainActivity extends AppCompatActivity {
    
    @BindView(R.id.btnButtonSheet)
    Button btnBottomSheet;
    
    @BindView(R.id.bottom_sheet)
    LinearLayout layoutBottomSheet;
    
    BottomSheetBehavior sheetBehavior;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
    
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
    
        sheetBehavior = BottomSheetBehavior.from(layoutBottomSheet);
    
    
        sheetBehavior.setBottomSheetCallback(new BottomSheetBehavior.BottomSheetCallback() {
            @Override
            public void onStateChanged(@NonNull View bottomSheet, int newState) {
                switch (newState) {
                    case BottomSheetBehavior.STATE_HIDDEN:
                        break;
                    case BottomSheetBehavior.STATE_EXPANDED: {
                        btnBottomSheet.setText("Close");
                    }
                    break;
                    case BottomSheetBehavior.STATE_COLLAPSED: {
                        btnBottomSheet.setText("Expand");
                    }
                    break;
                    case BottomSheetBehavior.STATE_DRAGGING:
                        break;
                    case BottomSheetBehavior.STATE_SETTLING:
                        break;
                }
            }
    
            @Override
            public void onSlide(@NonNull View bottomSheet, float slideOffset) {
    
            }
        });
    }
    
    @OnClick(R.id.btnButtonSheet)
    public void toggleBottomSheet() {
        if (sheetBehavior.getState() != BottomSheetBehavior.STATE_EXPANDED) {
            sheetBehavior.setState(BottomSheetBehavior.STATE_EXPANDED);
            btnBottomSheet.setText("Close Bottom sheet");
        } else {
            sheetBehavior.setState(BottomSheetBehavior.STATE_COLLAPSED);
            btnBottomSheet.setText("Expand Bottom sheet");
        }
    }
    }
    

提交回复
热议问题