问题
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