因此,当我强迫我的应用程序进入Light Theme时,我的应用程序的status bar
没有将其图标更改为黑暗。
我已经发现问题的根源,即status bar
中的图标不希望变暗,当我将SYSTEM_UI
标志LAYOUT_STABLE
和LAYOUT_FULLSCREEN
应用于Activity
时。当我删除这两个标志时,status bar
中的图标是正确的黑色。
但我上面这个“解决方案”的问题是我使用2个SYSTEM_UI
标志,以便让我的Activity的内容滚动到status bar
下面,我已经做了半透明。我没有想出另一种方法让我的status bar
接受透明度并且内容滚动在它下面,而不是使用我现在为这个SYSTEM_UI
的2个Activity
标志,再次是SYSTEM_UI_FLAG_LAYOUT_STABLE
和SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
。
有没有人知道如何解决这个问题?
也许有人可以告诉我一个可靠的方法让我的status bar
接受透明度并让内容在其下方滚动而不必使用SYSTEM_UI
标志?这可能会解决我的问题,我想......
我能想到的唯一与代码相关的代码是:
在我的MainActivity.java中,我有这个集合:
getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);`
在我的styles.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>
有任何想法吗?
好的,所以我自己想出来了。看起来我可能需要在调用SYSTEM_UI
之前设置setContentView
标志。虽然,它可能是这些变化的组合,使它对我有用 - 我不完全确定。
我改变的是在调用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
中选择了SharedPreference
按钮(对于黑暗模式/主题的工作方式相同),上面的Change theme
是我设置的toolbar
布尔值。
最后,对于我的MainActivity
的主题,我在我的styles.xml
中设置了这个样式(对于API 27及更高版本 - 对于较低的Android API'styles.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_UI
标志设置为LAYOUT_FULLSCREEN
和LAYOUT_STABLE
使得toolbar
位于statusbar
下方,我通过以下方式设置了一些合适的填充:
int statusBarHeight = getStatusBarHeight(this);
getStatusBarHeight
是一种用于解释不同Android设备上可能不同大小的statusbars
的方法(例如Pixel 3XL更大的statusbar
)。
我从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 statusBarHeight
的值并以编程方式将其用作我的toolbar
的上边距:
// 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);
最后,我确保我的应用程序的RecyclerView
正确地坐在statusbar
和toolbar
下面,我通过设置适当的填充来完成它,也是以编程方式:
// 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);
int itemOffsetPaddingSide
的值是4dp
,对于int actionBarSize
它是60dp
和int itemOffsetPaddingTop
,通过组合actionBarSize
和statusBarHeight
,我们得到56dp
加上特定设备的statusBar
高(因为检索到的值并在代码中进一步分配给statusBarHeight
)这个帖子)。
这一切都允许我的RecyclerView
的内容舒适地坐在statusbar
和toolbar
之下,并且当在两个下面滚动时也是可见的,因为bars
都有90% opacity
。
我已经独立地研究Java编程和Android应用程序开发近2年了,虽然我为这次来到的地方感到自豪,但我也不确定这是实现这种效果的最优雅方式在我的应用程序中但无论哪种方式,它都有效,如果您发现它对您的应用程序也有用,请告诉我!