如何在Android Java上更改导航栏和状态栏的颜色?

huangapple go评论68阅读模式
英文:

How to change the color of the navigation and status bar on Android Java?

问题

这是我第一次在 Android 中进行编程,我想要更改状态栏和导航栏的颜色,我阅读了文档,似乎很简单,但是对我来说不起作用。
我将展示我添加的内容,但如果您需要更多信息,我会发布出来。

Window window = getWindow();
window.setStatusBarColor(R.color.colorWhite);
window.setNavigationBarColor(R.color.colorWhite);

所有它做的只是将栏进行了明确的设置。

英文:

It is my first time programming in android and I want to change the color of the bars, I read the documentation it seems simple but it does not work for me.
I am going to show what I add but if you need more information I will post it.

    Window window = getWindow();
    window.setStatusBarColor(R.color.colorWhite);
    window.setNavigationBarColor(R.color.colorWhite);

All it does is clarify the bar

如何在Android Java上更改导航栏和状态栏的颜色?

答案1

得分: 0

从Java类:

通过调用资源获取颜色,可以这样做:

Window window = getWindow();
window.setStatusBarColor(getResources().getColor(R.color.colorWhite));
window.setNavigationBarColor(getResources().getColor(R.color.colorSome));

这个颜色可以从styles.xml文件中的AppTheme进行更改。它的值可以从属性<item name="colorPrimaryDark">#yourColor</item>进行更改。

styles.xml:

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <!-- 在这里自定义您的主题。 -->
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
</style>

它们的颜色在colors.xml中可用:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="colorPrimary">#3F51B5</color>
    <color name="colorPrimaryDark">#303F9F</color>
    <color name="colorAccent">#FF4081</color>
</resources>

阅读文档

英文:

From java class:

Get color by calling resources, do this way:

Window window = getWindow();
window.setStatusBarColor(getResources().getColor(R.color.colorWhite));
window.setNavigationBarColor(getResources().getColor(R.color.colorSome));

This color can be changed from your AppTheme in styles.xml file. It's value can be changed from attribute &lt;item name=&quot;colorPrimaryDark&quot;&gt;#yourColor&lt;/item&gt;

styles.xml:

&lt;style name=&quot;AppTheme&quot; parent=&quot;Theme.AppCompat.Light.DarkActionBar&quot;&gt;
    &lt;!-- Customize your theme here. --&gt;
    &lt;item name=&quot;colorPrimary&quot;&gt;@color/colorPrimary&lt;/item&gt;
    &lt;item name=&quot;colorPrimaryDark&quot;&gt;@color/colorPrimaryDark&lt;/item&gt;
    &lt;item name=&quot;colorAccent&quot;&gt;@color/colorAccent&lt;/item&gt;
&lt;/style&gt;

Their colors are available in colors.xml:

&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;resources&gt;
    &lt;color name=&quot;colorPrimary&quot;&gt;#3F51B5&lt;/color&gt;
    &lt;color name=&quot;colorPrimaryDark&quot;&gt;#303F9F&lt;/color&gt;
    &lt;color name=&quot;colorAccent&quot;&gt;#FF4081&lt;/color&gt;
&lt;/resources&gt;

Read the documentation.

答案2

得分: 0

我认为你可能想看看这个:

https://stackoverflow.com/questions/39341818/how-to-change-the-color-of-the-status-bar-in-android/39341866#:~:text=你可以通过在xml中添加来更改它。在API级别21+上,你可以使用代码中的setStatusBarColor()方法进行更改。

看起来你做得很好,但是似乎有些东西丢失了。

英文:

I think you might like to have a look at this

https://stackoverflow.com/questions/39341818/how-to-change-the-color-of-the-status-bar-in-android/39341866#:~:text=You%20can%20change%20it%20by,xml.&amp;text=On%20API%20level%2021%2B%20you,setStatusBarColor()%20method%20from%20code.

It appears that you're doing fine but something is missing

huangapple
  • 本文由 发表于 2020年9月19日 19:58:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/63968531.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定