首页 > 解决方案 > Snackbar:删除底部导航视图下方的额外空间

问题描述

我正在使用 BottomNavigationView 并在小吃栏中显示网络状态。

当 nosetTranslationY()被定义时,我在模拟器和物理设备中的快餐栏下方都有一个额外的空间。

我面临的问题是:固定一个值setTranslationY()可以为某些设备腾出额外的空间,并为其他设备切割小吃栏的底部。

所以我的问题是:如何正确设置适合所有设备的值?

下面是定制的小吃店:

public class CustomSnackbar extends BaseTransientBottomBar<CustomSnackbar> {


    /**
     * Constructor for the transient bottom bar.
     *
     * @param parent              The parent for this transient bottom bar.
     * @param content             The content view for this transient bottom bar.
     * @param contentViewCallback The content view callback for this transient bottom bar.
     */
    protected CustomSnackbar(@NonNull ViewGroup parent, @NonNull View content, @NonNull com.google.android.material.snackbar.ContentViewCallback contentViewCallback) {
        super(parent, content, contentViewCallback);
    }

    public CustomSnackbar setText(CharSequence message) {
        TextView text = (TextView) getView().findViewById(R.id.snackbar_text);
        text.setText(message);
        return this;
    }

    public CustomSnackbar setBackgroundColor(int color) {
        getView().setBackgroundColor(color);
        getView().setAlpha(1f);
        return this;
    }

    private static class CustomContentViewCallback implements com.google.android.material.snackbar.ContentViewCallback {

        private View view;

        public CustomContentViewCallback(View view) {
            this.view = view;
        }

        @Override
        public void animateContentIn(int delay, int duration) {

        }

        @Override
        public void animateContentOut(int delay, int duration) {

        }
    }

    public static CustomSnackbar make(ViewGroup parent, int duration) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.custom_snackbar, parent, false);

        CustomContentViewCallback viewCallback = new CustomContentViewCallback(view);
        CustomSnackbar customSnackbar = new CustomSnackbar(parent, view, viewCallback);
        customSnackbar.getView().setPadding(0,0,0,0);

        CoordinatorLayout.LayoutParams params = (CoordinatorLayout.LayoutParams) customSnackbar.getView().getLayoutParams();
        params.setMargins(0, 0, 0, 0);
        customSnackbar.getView().setLayoutParams(params);
        customSnackbar.getView().setTranslationY(18f); // THE FIXED VALUE IS CAUSING THE ISSUE

        customSnackbar.setDuration(duration);
        return customSnackbar;
    }

}

下面是活动布局:

<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/main_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context=".MainActivity">

    <fragment
        android:id="@+id/main_fragment"
        android:name="androidx.navigation.fragment.NavHostFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:defaultNavHost="true"
        android:layout_marginBottom="?attr/actionBarSize"
        app:navGraph="@navigation/mainnav_graph" />

    <com.google.android.material.bottomnavigation.BottomNavigationView
        android:id="@+id/main_bottomnav"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        android:background="@color/colorAccent"
        app:itemIconTint="@drawable/botton_item_color"
        app:itemTextColor="@drawable/botton_item_color"
        app:layout_dodgeInsetEdges="bottom"
        app:menu="@menu/main_navmenu">

    </com.google.android.material.bottomnavigation.BottomNavigationView>

</androidx.coordinatorlayout.widget.CoordinatorLayout>

我很感激任何帮助!

PS

setOnApplyWindowInsetsListener()似乎是要走的路,但我收到一条错误消息,因为我的最低 API 级别是 19。

标签: androidandroid-snackbar

解决方案


推荐阅读