NoActionBar 主题中的 snackbar 高度加倍

snackbar height is doubled in NoActionBar themes

我使用 NoActionBar 来 activity 像这样:

 <style name="Theme.AppCompat.Light.NoActionBar.FullScreen" parent="@style/Theme.AppCompat.Light.NoActionBar">
    <item name="android:windowNoTitle">true</item>
    <item name="android:windowActionBar">false</item>
    <item name="android:windowFullscreen">true</item>
    <item name="android:windowContentOverlay">@null</item>
</style>

我试着像这样设置全屏:

 @Override
public void onWindowFocusChanged(boolean hasFocus) {
    super.onWindowFocusChanged(hasFocus);
    AppLog.Log(TAG, "hasFocus: " + hasFocus);
    if (hasFocus) {
        getWindow().getDecorView().setSystemUiVisibility(
                View.SYSTEM_UI_FLAG_LOW_PROFILE
                | View.SYSTEM_UI_FLAG_FULLSCREEN
                | View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
                | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION);
    }
}

但我的小吃店高度是这样加倍的:

为什么在 NoActionBar 主题中 snackbar 高度加倍?

您的 snackbar 正在占用之前 Actionbar 的 space。用这个来修复它:

final Snackbar snackbar = Snackbar.make(findViewById(R.id.fullscreen_content),
            message, Snackbar.LENGTH_LONG);

View snackbarView = snackbar.getView();

//One way of getting the height of the navBar
int navbarHeight = getNavigationBarSize(this).y;

//The LayoutParams of the layout you are using
CoordinatorLayout.LayoutParams parentParams = (CoordinatorLayout.LayoutParams) snackbarView.getLayoutParams();
    parentParams.setMargins(0, 0, 0, 0 - ScreenUtils.getNavigationBarHeight(activity));
    snackbarView.setLayoutParams(parentParams);

snackbar.show();

解释: 有两种获取导航栏高度的方法。

getNavigationBarSize(this).y;

ScreenUtils.getNavigationBarHeight(activity));

使用其中一个,然后从父布局的 底部边距 中减去该数量。

如果您使用相对或线性布局,请使用适当的布局参数,例如 RelativeLayout.LayoutParamsLinearLayout.LayoutParams