簡體   English   中英

android-透明的導航欄,但不是狀態欄

[英]android - Transparent Navigation Bar but not Status Bar

我不確定是否可以,但是我試圖使導航欄透明而不使狀態欄透明。 稍后的原因是我有一個工具欄,否則它會在狀態欄后面繪制。

現在,我正在使用一個falseStatusBar布局,該布局顯示在狀態欄之前,因此對用戶而言一切都很好:

    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);

唯一的問題是,在進行多任務處理時,縮略圖看起來不佳,因為工具欄不在應有的位置。

並不是我想要的,但我發現了使用半透明導航欄的解決方法。 最終代碼如下所示:

    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);
    }

將頂部填充添加到活動的主布局的位置。

如果有人知道如何使用透明導航欄執行類似操作,那就太好了,但否則我對我的解決方案感到滿意。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM