亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

使用LAYOUT_FULLSCREEN或LAYOUT_STABLE UI 標志時

使用LAYOUT_FULLSCREEN或LAYOUT_STABLE UI 標志時

弒天下 2022-09-22 10:44:49
因此,當我強制我的應用程序進入淺色主題時,我的應用程序沒有將其圖標更改為深色,因此我遇到了一個問題。status bar我發現了問題的根源,即當我擁有標志并應用于 .當我刪除這兩個標志時,中的圖標是適當的黑暗。status barSYSTEM_UILAYOUT_STABLELAYOUT_FULLSCREENActivitystatus bar但是我對上面的這個“解決方案”的問題是,我使用2個標志,以便使我的活動內容滾動到下面,我已經使它半透明。我還沒有想出另一種方法來使我接受透明,并在其下方滾動內容,除了使用我目前擁有的2標志,這些標志再次和。SYSTEM_UIstatus barstatus barSYSTEM_UIActivitySYSTEM_UI_FLAG_LAYOUT_STABLESYSTEM_UI_FLAG_LAYOUT_FULLSCREEN有沒有人知道我如何解決這個問題?也許有人可以向我展示一種可靠的方法來讓我接受透明度,并讓內容滾動到它下面,而不必使用標志?這可能會解決我的問題,我認為...status barSYSTEM_UI我能想到的唯一與共享相關的代碼是這樣的:在我的主活動.java中,我有這個集合:getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);`在我的風格中.xml,我為以下主題設置了這個主題:Light Theme<!-- Toolbar/NoActionBar variant of default Light Theme --> <style name="AppTheme_Light_NoActionBar" parent="Theme.AppCompat.Light.NoActionBar">     <item name="colorPrimary">@color/pure_white_transparent</item>     <item name="colorPrimaryDark">@color/pure_white_transparent</item>     <item name="colorAccent">@color/colorAccent</item>     <item name="android:windowActionBarOverlay">true</item>     <item name="android:windowTranslucentStatus">false</item>     <item name="android:windowDrawsSystemBarBackgrounds">true</item>     <item name="android:statusBarColor">@color/pure_white_transparent</item>     <item name="android:windowLightStatusBar">true</item>     <item name="android:navigationBarColor">@color/pure_white</item>     <item name="android:windowLightNavigationBar">true</item> </style>有什么想法嗎?
查看完整描述

1 回答

?
蝴蝶刀刀

TA貢獻1801條經驗 獲得超8個贊

好吧,所以我自己想通了。看起來我可能需要在調用之前設置標志。雖然,它可能是這些變化的組合,使它對我有用 - 我不完全確定。SYSTEM_UIsetContentView


我更改的是使用這些標志,然后調用:setContentView


if (lightMode) {

        setTheme(R.style.AppTheme_Light_NoActionBar);

        getWindow().getDecorView().setSystemUiVisibility

               (View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR |

                View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR | 

                View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN |

                View.SYSTEM_UI_FLAG_LAYOUT_STABLE);

    }

lightMode上面是我設置的布爾值,如果用戶在我的應用中選擇了按鈕(對于深色模式/主題的工作方式相同)。SharedPreferenceChange themetoolbar


最后,對于我的主題,我在我的(對于API 27及更高版本 - 對于較低的Android API看起來有點不同)MainActivitystyles.xmlstyles.xml


<!-- Toolbar/NoActionBar variant of default Light Theme -->

<style name="AppTheme_Light_NoActionBar" parent="Theme.AppCompat.Light.NoActionBar">

    <item name="colorPrimary">@color/pure_white_transparent</item>

    <item name="colorPrimaryDark">@color/pure_white_transparent</item>

    <item name="colorAccent">@color/colorAccent</item>

    <item name="android:windowActionBarOverlay">true</item>

    <item name="android:windowTranslucentStatus">false</item>

    <item name="android:windowDrawsSystemBarBackgrounds">true</item>

    <item name="android:statusBarColor">@color/pure_white_transparent</item>

    <item name="android:navigationBarColor">@color/pure_white_transparent</item>

</style>

因為在我看來,將標志設置為 并使得坐在 下面,我通過以下方式設置了一些適當的填充:SYSTEM_UILAYOUT_FULLSCREENLAYOUT_STABLEtoolbarstatusbar


int statusBarHeight = getStatusBarHeight(this);其中 是用于解釋不同 Android 設備上可能大小不同的方法(例如像素 3XL 的較大 )。getStatusBarHeightstatusbarsstatusbar


我從StackOverflow上的其他地方得到了這個工作方法,但忘記了在哪里,所以如果有人知道,請隨時鏈接它 - 下面的方法:


    // Gets the StatusBar's height of the particular display.

    public static int getStatusBarHeight(final Context context) {

        final Resources resources = context.getResources();

        final int resourceId = resources.getIdentifier("status_bar_height", "dimen", "android");

        if (resourceId > 0) {

            return resources.getDimensionPixelSize(resourceId);

        } else {

            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {

                return (int) Math.ceil(24 * resources.getDisplayMetrics().density);

            } else {

                return (int) Math.ceil(25 * resources.getDisplayMetrics().density);

            }

        }

}

然后,我采用的值并將其用作編程的上邊距:int statusBarHeighttoolbar


// Setup the values for the toolbar's layout parameters.

    CoordinatorLayout.LayoutParams toolbarParams = new CoordinatorLayout.LayoutParams(

            CoordinatorLayout.LayoutParams.MATCH_PARENT,

            CoordinatorLayout.LayoutParams.WRAP_CONTENT

    );

    toolbarParams.setMargins(0, statusBarHeight, 0, 0);


    // Find, Assign, and Setup the Toolbar to take place of the Actionbar.

    // Then inflate the proper menu to be used on-top of it, and set its layout parameters

    // which have been set in the code above.

    Toolbar toolbar = findViewById(R.id.toolbar);

    setSupportActionBar(toolbar);

    toolbar.inflateMenu(R.menu.menu_main);

    getSupportActionBar().setDisplayShowTitleEnabled(true);

    toolbar.setLayoutParams(toolbarParams);

最后,我已確保我的應用程序正確位于 和 的下方,這是我通過設置適當的填充來實現的,也是以編程方式完成的:RecyclerViewstatusbartoolbar


// Get and set the values for the OffsetPadding for the RecyclerView to int.

    int itemOffsetPaddingSide = (int) getResources().getDimension(R.dimen.item_offset);

    int actionBarSize = (int) getResources().getDimension(R.dimen.actionbarSizeWithExtraPadding);

    int itemOffsetPaddingTop = actionBarSize + statusBarHeight;

    // Set all the OffsetPadding values to the RecyclerView programmatically, so that

    // all the UI elements are padding properly, taking into account the devices screen

    // density/size/resolution!

    mRecyclerView.setPadding(itemOffsetPaddingSide, itemOffsetPaddingTop, itemOffsetPaddingSide, itemOffsetPaddingSide);

的值是 ,for it 和 for ,通過組合和 ,我們得到加號,無論特定設備有多高(因為在帖子后面的代碼中檢索和分配的值)。int itemOffsetPaddingSide4dpint actionBarSize60dpint itemOffsetPaddingTopactionBarSizestatusBarHeight56dpstatusBarstatusBarHeight


這一切都允許 我的內容舒適地位于 和 的下方,并且在滾動它們下面時也是可見的,因為兩者都有 .RecyclerViewstatusbartoolbarbars90% opacity


我已經獨立學習Java編程和Android應用程序開發近2年了,雖然我為我這次取得的成就感到自豪,但我也不確定這是否是在我的應用程序中實現這種效果的最優雅方式。但無論哪種方式,它都可以工作,如果你發現它對你的應用程序也很有用,請告訴我!


查看完整回答
反對 回復 2022-09-22
  • 1 回答
  • 0 關注
  • 123 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號