代码之家  ›  专栏  ›  技术社区  ›  nichtwer

android-透明导航栏,但不是状态栏

  •  1
  • nichtwer  · 技术社区  · 7 年前

    我不确定这是否可行,但我正在尝试使导航栏透明,而不使状态栏透明。之所以选择后者,是因为我有一个工具栏,否则会在状态栏后面绘制。

    现在,我正在使用一个fakeStatusBar布局,它显示在状态栏之前,这样用户就可以看到所有内容:

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT){
            Window window = getWindow();
            window.setFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS, WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
    
            //now the status bar is transparent too, which we have to fix
    
            //first we get status bar height
            int statusBarHeight = 0;
            int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");
            if (resourceId > 0) {
                statusBarHeight = getResources().getDimensionPixelSize(resourceId);
            }
    
            //then we get the full width
            Display display = getWindowManager().getDefaultDisplay();
            Point size = new Point();
            display.getSize(size);
            int screenWidth = size.x;
    
            //then we set that height to the fake item in the layout
            LinearLayout fakeStatusBar = findViewById(R.id.fakeStatusBar);
            ViewGroup.LayoutParams params = fakeStatusBar.getLayoutParams();
            params.height = statusBarHeight;
            params.width = screenWidth;
            fakeStatusBar.setLayoutParams(params);
    

    唯一的问题是,当要进行多任务处理时,缩略图看起来不太好,因为工具栏不是它应该位于的位置。

    1 回复  |  直到 7 年前
        1
  •  2
  •   nichtwer    7 年前

    这并不完全是我想要的,但我找到了一个使用半透明导航栏的解决方法。最终代码如下所示:

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT){
    
            //set navigation bar translucent
            Window window = getWindow();
            window.setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION, WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
            //now the status bar is translucent too, which we have to fix
    
            //first we get status bar height
            int statusBarHeight = 0;
            int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");
            if (resourceId > 0) {
                statusBarHeight = getResources().getDimensionPixelSize(resourceId);
            }
    
            //then set it as top padding for the main layout
            ConstraintLayout mainLayout = (ConstraintLayout) findViewById(R.id.mainLayout);
            mainLayout.setPadding(0,statusBarHeight,0,0);
        }
    

    将顶部填充添加到活动的主布局中。

    如果有人知道如何使用透明的导航栏做类似的事情,那就太好了,但我对我的解决方案很满意。