Change status bar text color when primaryDark is white

余生颓废 提交于 2020-05-09 18:16:10

问题


I am trying to reproduce the behaviour of Google Calendar application:

but I have not found a way to change the status text color. If i set the colorPrimaryDark as white I cannot see the icons neither text of status bar due their color is white as well.

Is there any way to change the status bar text color?

Thanks in advance


回答1:


I'm not sure what API level your trying to target, but if you can use API 23 specific stuff, you can add the following to your AppTheme styles.xml:

<item name="android:statusBarColor">@color/colorPrimaryDark</item>
<item name="android:windowLightStatusBar">true</item>

when android:windowLightStatusBar is set to true, status bar text color will be able to be seen when the status bar color is white, and vice-versa when android:windowLightStatusBar is set to false, status bar text color will be designed to be seen when the status bar color is dark.

Example:

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
    <!-- Status bar stuff. -->
    <item name="android:statusBarColor">@color/colorPrimaryDark</item>
    <item name="android:windowLightStatusBar">true</item> 
</style>



回答2:


you can do that programmatically like this answer

just add this

getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);



回答3:


it's very simple:

getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);//  set status text dark
getWindow().setStatusBarColor(ContextCompat.getColor(BookReaderActivity.this,R.color.white));// set status background white

and vice versa:

getWindow().setStatusBarColor(ContextCompat.getColor(BookReaderActivity.this, R.color.black));
View decorView = getWindow().getDecorView(); //set status background black 
decorView.setSystemUiVisibility(decorView.getSystemUiVisibility() & ~View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR); //set status text  light



回答4:


As previous, the SYSTEM_UI_FLAG_LIGHT_STATUS_BAR do the work in my case, don't forget to set for higher than API 22.

add this to oncreate after the setContentView:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
    getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
}



回答5:


getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);//  set status text dark

getWindow().setStatusBarColor(ContextCompat.getColor(MainActivity.this,R.color.colorPrimaryDark));// set status background white

It works for me




回答6:


In your activity onCreate() method, paste the following code after the setContentView(R.layout.activity_generic_main);

Here is the sample code below.

public class GenericMain extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_generic_main);
        getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);

    }
}



回答7:


Try this once.

In your activity onCreate() method, paste the following code.

try {
        if (android.os.Build.VERSION.SDK_INT >= 21) {
                Window window = getWindow();
                window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
                window.setStatusBarColor(ContextCompat.getColor(this, R.color.color_red));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

Note: color_red - is the status bar colour.



来源:https://stackoverflow.com/questions/38382283/change-status-bar-text-color-when-primarydark-is-white

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!