get height of snackbar support library

别等时光非礼了梦想. 提交于 2019-12-11 12:13:38

问题


I'm working with Snackbar and fab support library. I want to get height of Snackbar that gives correct height value with single line and multi line message.

var=mysnackbar.gethight();

but this code does not work because snackbar does have gethight().


回答1:


Snackbar will only has height after the show() method called. Try call:

mSnackbar.show();
int height = mSnackbar.getView.getHeight();

Or you can use the callback to listen for Snackbar is shown event and return its height as shown below:

mSnackbar.setCallback(new Snackbar.Callback {
    @Override
    Public void onDismissed(Snackbar snackbar, int event) {
    }

    @Override
    Public void onShown(Snackbar snackbar) {
        int height = snackbar.getView.getHeight();
    }
)
mSnackbar.show();



回答2:


This should work possibly. Will give the height of your Snackbar in pixels.

int height = yourSnackbar.getView().getHeight();

The reference documentation : https://developer.android.com/reference/android/support/design/widget/Snackbar.html#getView()

https://developer.android.com/reference/android/view/View.html#getHeight()




回答3:


Adding to answer by @Grace Coder

Snackbar.setCallback() has been deprecated.

Now you need to use Snackbar.addCallback()

    View parentView = findViewById(R.id.activity_main);

    Snackbar snackbar = Snackbar.make(parentView, "Hello there.", Snackbar.LENGTH_SHORT);
    snackbar.addCallback(new BaseTransientBottomBar.BaseCallback<Snackbar>() {
        @Override
        public void onShown(Snackbar transientBottomBar) {
            int height = transientBottomBar.getView().getHeight();
            parent.setPadding(0, 0, 0, height);

            super.onShown(transientBottomBar);
        }

        @Override
        public void onDismissed(Snackbar transientBottomBar, int event) {
            parent.setPadding(0, 0, 0, 0);

            super.onDismissed(transientBottomBar, event);
        }
    });
    snackbar.show();


来源:https://stackoverflow.com/questions/33544931/get-height-of-snackbar-support-library

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!