如何以编程方式更改 Android L 中的原色?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25354558/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 09:22:26  来源:igfitidea点击:

How to programmatically change the primary color in Android L?

androidandroid-5.0-lollipop

提问by ebtokyo

Is there a way to change programmatically the primary colors. I would like to do it in code depending on the screen/state of the app.

有没有办法以编程方式更改原色。我想根据应用程序的屏幕/状态在代码中执行此操作。

Currently I can only set the colors in the theme (static) :

目前我只能在主题中设置颜色(静态):

<item name="android:colorPrimary">@color/primary_color</item>
<item name="android:colorPrimaryDark">@color/dark_color</item>
<item name="android:colorBackground">@android:color/white</item>
<item name="android:colorAccent">@color/primary_color</item>
<item name="android:colorControlHighlight">@color/primary_color</item>

采纳答案by yogurtearl

You can, of course, implement custom subclasses of View that have methods for setting colors.

当然,您可以实现具有设置颜色方法的 View 的自定义子类。

You can also define multiple themes with you various color schemes.

您还可以使用各种配色方案定义多个主题。

Viewslook up theme information from the context when they are created. So to change the styles applied from a theme you will have to recreate your view hierarchy with a context that uses the right theme.

视图在创建时从上下文中查找主题信息。因此,要更改从主题应用的样式,您必须使用使用正确主题的上下文重新创建视图层次结构。

One way to do that, is to create a new ContextThemeWrapperand then get a LayoutInflator that uses that theme wrapper, remove the old version of your layout and re-inflate your layout.

一种方法是创建一个新的ContextThemeWrapper,然后获取一个使用该主题包装器的 LayoutInflator,删除旧版本的布局并重新膨胀您的布局。

Roughly:

大致:

ContextThemeWrapper themeWrapper = new ContextThemeWrapper(this, R.style.AppThemeWithColorScheme2);
LayoutInflater layoutInflater = LayoutInflater.from(themeWrapper);
viewContainer.removeAllViews();
layoutInflater.inflate(R.layout.my_layout, viewContainer, true );

If you are using Action Bar, that may be a bit more tricky, because the Action Bar is created once per activity.

如果您使用的是 Action Bar,那可能会有点棘手,因为 Action Bar每个 Activity创建一次

回答by Vaishali Malviya

USe this code for setting toolbarcolor and status bar (darker toolbar color)

使用此代码设置工具栏颜色和状态栏(工具栏颜色较深)

toolbar.setBackgroundColor(toolbarColor);
factor=0.8f; 
int a = Color.alpha(toolbarcolor);
int r = Math.round(Color.red(toolbarcolor) * factor);
int g = Math.round(Color.green(toolbarcolor) * factor);
int b = Math.round(Color.blue(toolbarcolor) * factor);
int statusColor=Color.argb(a,
        Math.min(r, 255),
        Math.min(g, 255),
        Math.min(b, 255));
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
    Window window = MainActivity.this.getWindow();
    window.setStatusBarColor(statusColor);
}

回答by Crafty Handy

This is most practical with no error, there is no need to do any extra coding, just go to android tree project

这个最实用,没有错误,不需要做任何额外的编码,直接去android tree项目

res > values > colors then edit these codes:

res > values > colors 然后编辑这些代码:

 <resources>
    <color name="colorPrimary">#008577</color>
    <color name="colorPrimaryDark">#00574B</color>
    <color name="colorAccent">#D81B60</color>
</resources>

add these under style:

在样式下添加这些:

<style name="AppTheme.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>

add into manifest just after ".MainActivity" :

在 ".MainActivity" 之后添加到清单中:

android:theme="@style/AppTheme.NoActionBar">

回答by user12128015

This change everything in text

这改变了文本中的一切

<item name="android:textColorPrimary">@color/textGrey</item>
    <item name="android:textColorSecondary">@color/textGrey</item>
    <item name="android:textColorTertiary">@color/textGrey</item>
    <item name="android:listDivider">@color/textGrey</item>