如何获得透明软键栏和透明状态栏的高度?

How to get heights of a transparent softkeys bar and transparent status bar?

我有一个 activity 包含 RecyclerView 和透明状态栏(包含 wifi 信号等的状态栏)和软键栏。当然,RecyclerView 中的项目数量是未定义的,我需要第一个具有标准上边距加上状态栏高度,最后一个具有标准下边距加上软键栏的高度。这是必需的,否则当滚动条分别位于顶部或底部时,第一个和最后一个项目将被栏部分覆盖。

这里是我如何以编程方式处理边距的示例代码:

DisplayMetrics metrics = new DisplayMetrics();
        activity.getWindowManager().getDefaultDisplay().getMetrics(metrics);

        topSpace = new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        bottomSpace = new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        standardSpace = new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);

        topSpace.setMargins(     (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 8,    metrics),
                                 (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 8 + 24, metrics),
                                 (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 8,    metrics),
                                 (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 8,    metrics));

        bottomSpace.setMargins(  (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 8,    metrics),
                                 (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 8,    metrics),
                                 (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 8,    metrics),
                                 (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 8 + 56, metrics) +
                                         getSoftkeysHeight(activity));

        standardSpace.setMargins((int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 8,    metrics),
                                 (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 8,    metrics),
                                 (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 8,    metrics),
                                 (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 8,    metrics));

我正在尝试使用 但它们似乎对我没有帮助,因为屏幕的可绘制表面确实是整个屏幕...

您可以使用RecyclerView.ItemDecoration修改特定项目的边距。

  1. 创建一个扩展 RecyclerView.ItemDecoration 的 class :

    public static class SimpleItemDecorator extends RecyclerView.ItemDecoration {
    
    private int regularMargin;
    private int lastPosition;
    private int statusBarHeight;
    private int navigationBarHeight;
    
    public SimpleItemDecorator(Activity activity, int recyclerViewItemsCount) {
        // get the regular margin
        regularMargin = activity.getResources().getDimensionPixelSize(R.dimen.regularMargin);
    
        // determine the last position
        lastPosition = recyclerViewItemsCount - 1;
    
        // get the height of the status bar
        final Rect rectangle = new Rect();
        final Window window = activity.getWindow();
        window.getDecorView().getWindowVisibleDisplayFrame(rectangle);
        statusBarHeight = rectangle.top;
    
        // get the height of the navigation bar
        final int redId = resources.getIdentifier("navigation_bar_height", "dimen", "android");
        navigationBarHeight = (redId > 0) ? resources.getDimensionPixelSize(resId) : 0;
    
    }
    
    @Override
    public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
        // modify the margins
        final int position = parent.getChildAdapterPosition(view);
        if (position == 0) {
            outRect.top = statusBarHeight + regularMargin;
        } else if (position == lastPosition) {
            outRect.bottom = navigationBar + regularMargin;
        }
    }
    

    }

  2. 将创建的 ItemDecorator 添加到 RecyclerView :

    final int recyclerViewItemsCount = 6;
    final SimpleItemDecorator itemDecorator = new SimpleItemDecorator(this, recyclerViewItemsCount);
    recyclerView.addItemDecorator(itemDecorator);