首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >android -透明导航栏而不是状态栏

android -透明导航栏而不是状态栏
EN

Stack Overflow用户
提问于 2018-01-28 00:03:53
回答 1查看 2.1K关注 0票数 1

我不确定这是否可能,但我试图使导航栏透明,而不使状态栏透明。后者的原因是,我有一个工具栏,否则会在状态栏后面。

现在,我使用的是一个显示在状态栏前面的fakeStatusBar布局,所以对用户来说一切看起来都很好:

代码语言:javascript
运行
复制
    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);

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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-01-29 22:44:29

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

代码语言:javascript
运行
复制
    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);
    }

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

如果有人知道如何使用透明导航条来做类似的事情,那就太完美了,否则我会对我的解决方案感到满意。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48481834

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档