Modify actionbar, statusbar color in the same activity in 6.0

In an activity, dynamically modify the color of the actionbar and status bar in code
Seriously why use actionbar. It feels difficult to use, and the basic requirement to change the bug is to modify the original code unchanged. If you add a custom actionbar to customize the view, it will feel very troublesome.

1. Status
1. The default is to set the style of activity startup
(including the display effect of the status bar, the display effect of actionbar or custom multi-toolbar, etc.)

2. After changing the animation according to the requirements, the interface needs to change and need to be modified, because in order to adapt to the effect of the next interface (requirements)

2. Solution:
2.0 Here, I will reiterate the setting method that the status bar and app style are consistent with the different versions of the previous version.

4.4 - The transparent status bar set in 5.0 (also set in the layout xml)
values-v19/style.xml

     <style name="XXXX" parent="Theme.XXXX">
        <item name="android:windowTranslucentStatus">true</item>
        <item name="android:windowTranslucentNavigation">true</item>
       </style>

5.0 or above (when the statusBarColor and colorPrimary attributes exist at the same time, the color of the status bar is based on the statusBarColor attribute)
values-v21/style.xml

     当你的主题继承的是 普通的 就设置 当statusBarColor 就好了
       <style name="XXX" parent="XXXX">
         <item name="android:windowTranslucentStatus">false</item>   
         <!-- 其实为了适配的话这里就设置false 下面设置颜色比较好-->
         <item name="android:windowTranslucentNavigation">true</item>
         <!--Android 5.x  把颜色设置透明 , 或者直接设置状态栏的颜色与app风格一致也可以-->
         <item name="android:statusBarColor">@android:color/transparent</item>
       </style>

If inheriting a Material style theme
@android:style/Theme.Material (dark version)
@android:style/Theme.Material.Light (light version)
@android:style/Theme.Material.Light.DarkActionBar

The corresponding AppCompatTheme:
Theme.AppCompat
Theme.AppCompat.Light
Theme.AppCompat.Light.DarkActionBar

           <item name="colorPrimary">@android:color/holo_blue_bright</item>
           <item name="colorPrimaryDark">@android:color/holo_blue_bright</item>

2.1 Modify the status bar 6.0 (the focus of this requirement)

(1) Set the status bar color in the code

Window window = this.getWindow();
window.getDecorView().setSystemUiVisibility( View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN| View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);     
window.setStatusBarColor(this.getResources().getColor(R.color.TCT_colorPrimary));

(2) Modify the background color of the actionbar

getActionBar().setBackgroundDrawable(new ColorDrawable(android.R.color.transparent));

The above code is to make the background of the actionbar transparent, the effect is still good after seeing it, and it shows more and more background colors (but I need the actionbar to be a different color, not the same as the overall background layout color. )

Then I drew the tiger according to the cat, and I was about to cry.

getActionBar().setBackgroundDrawable(new ColorDrawable(R.color.BackBar));

In the end, the experiment found the correct way to display the correct color. I don't know why it has to be written like this. Whoever knows, please tell me why

solution:

 getActionBar().setBackgroundDrawable(this.getBaseContext().getResources().getDrawable(R.drawable.BackBar));

(3) Modify the color of the actionbar font
This is the real dog. I remember that there used to be a method that can set multiple attributes. In the code
I am modifying now, he uses the setTitle() method to set multiple titles. Here I have a problem, the default inherited style of the current actandroid:windowIsTranslucentivity is "@ style/Theme.Material.Light.DarkActionBar” (of course, some property modifications were made after inheritance) Then the code uses setTitle() to set the title, and the actionbar itself also has a getActionBar().setTitle(spanText); The method of setting the title
I 2 I tried each of them separately, the effect is the same, it should be because of the theme, so the default method of setTitle() is to set the title of the actionbar,
and then I did a special experiment, and sure enough (I really didn't notice it before) However, there are actionbars that set the title directly with the actionbar. Sure enough, you can learn a lot by looking at other people's code, or have more understanding of a lot of things)
setTitle(spanText);
System.out.println( getActionBar( ).getTitle());
I want to modify the color, and now I only see this method
setTitleColor(),
but it is useless to set it, all kinds of useless, and pissed off the baby (acitvity loads and sets multiple colors at the beginning and sets it in the style Well, I can't find it after a long time of checking, if anyone knows how to set it, thank you) I
finally found another solution

solution:

SpannableString spanText = new SpannableString("已完成");
// 设置字体的样式的
// spanText.setSpan(new TypefaceSpan("sans-serif"), 0, spanText.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
// 设置颜色
spanText.setSpan(new ForegroundColorSpan(this.getResources().getColor(R.color.list_primary_text_color)),  0, spanText.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
       getActionBar().setTitle(spanText);

(4) The problem of the color of the font in the status bar
has also been read on the Internet. I also know that our acitivty first loads and creates a blue main (actionbar, statusbar), and the title and status bar fonts are white. Because of the logic, we need to turn the actionbar and statusbar into gray-white. Of course, your status bar The font must also be changed, otherwise the white one will be invisible (same as other gray and white interface effects).
But the truth is, nothing has changed.
Then I saw that the style in the module has an attribute
android:windowLightStatusBar and I checked it. The Internet said that if it is set to true, it will dynamically change the font and icon color of the status bar to optimize it.
I set it to
name=”android: "windowLightStatusBar"
result is still not used, and the status bar is still white.

Final solution:

Android 6.0 provides bright status bar mode, the setting code is as follows:
add flag when getting window

Window window = this.getWindow();       
window.getDecorView().setSystemUiVisibility( View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN| View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);

Third, and then understand this aspect. The
default statusbar of the activity's style system is black, and then the font is white.
The current activity, by default, the style
background is blue, the
title is
white , and the status bar font is also white (effect picture)

The style attribute set must be like this

android:windowIsTranslucent  false
android:windowLightStatusBar  true 

3.1
Let the 6.0 status bar font adapt to automatically turn black and white,
where windowIsTranslucent is translucent and transparent property is set to false,
windowLightStatusBar customizes the status bar font optimization to true

3.2

name="android:windowIsTranslucent"  true 
name="android:windowLightStatusBar" true

name="android:windowIsTranslucent" true
name="android:windowLightStatusBar" false

After experiments, in 6.0, the above two attribute settings, the font color of the status bar is always white, no matter what color the status bar is (so this can meet the needs of our current page, the status bar font needs to be white) according to my Understand that if the window is set to set the transparent property, the color selection on the status bar is not easy to choose. Therefore, the property of windowLightStatusBar is set in the code behind the same activity, and the font color of the status bar cannot be changed.

3.3 So in order to set the status bar font of different colors in the same activity

name=”android:windowIsTranslucent” true must be false
name=”android:windowLightStatusBar” false This setting is false
if you want to force it to be white, you can set it like this (because it will automatically change color if it is true)

Then when you need to change color

在代码中设置如下代码即可
Android 6.0 中提供了亮色状态栏模式,设置代码如
    在获取window的时候 添加flag
    Window window = this.getWindow();       
    window.getDecorView().setSystemUiVisibility( View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN| View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325920988&siteId=291194637