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年了,雖然我為我這次取得的成就感到自豪,但我也不確定這是否是在我的應用程序中實現這種效果的最優雅方式。但無論哪種方式,它都可以工作,如果你發現它對你的應用程序也很有用,請告訴我!
添加回答
舉報