Android Material:状态栏颜色不会改变
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26496411/
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
Android Material: Status bar color won't change
提问by FrancescoAzzola
I'm developing an simple app to test the material design. I'm using com.android.support:appcompat-v7:21.0.0
and my activity looks like:
我正在开发一个简单的应用程序来测试材料设计。我正在使用com.android.support:appcompat-v7:21.0.0
,我的活动看起来像:
public class MyActivity extends ActionBarActivity {
...
}
The layout is defined as:
布局定义为:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MyActivity">
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="128dp"
android:minHeight="?attr/actionBarSize"
android:background="?attr/colorPrimaryDark"/>
</LinearLayout>
Now i defined my theme following material guidelines:
现在我按照材料指南定义了我的主题:
<style name="AppTheme" parent="Theme.AppCompat.NoActionBar">
<item name="colorPrimary">@color/colorPrimary500</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark700</item>
</style>
I'd like to change the status bar color in pre Android 5 and set it to colorPrimaryDark
but i can't find the way. I tried using:
我想更改 Android 5 之前的状态栏颜色并将其设置为,colorPrimaryDark
但我找不到方法。我尝试使用:
getWindow().setStatusBarColor(..)
but setStatusBar color is available from level 21.
Why if i define a colorPrimaryDark
in my theme and use appcompact the status bar doesn't change color?
Anyone can help?
但是 setStatusBar 颜色从 21 级开始可用。为什么如果我colorPrimaryDark
在我的主题中定义 a并使用 appcompact 状态栏不会改变颜色?任何人都可以帮忙吗?
回答by Alex Lockwood
The status bar is a system window owned by the operating system. On pre-5.0 Android devices, applications do not have permission to alter its color, so this is not something that the AppCompat library can support for older platform versions. The best AppCompat can do is provide support for coloring the ActionBar
and other common UI widgets within the application.
状态栏是操作系统拥有的系统窗口。在 5.0 之前的 Android 设备上,应用程序无权更改其颜色,因此 AppCompat 库无法支持旧平台版本。AppCompat 可以做的最好的事情是为ActionBar
应用程序中的和其他常见 UI 小部件着色提供支持。
回答by Tom
While colouring the status bar is not supported <5.0, on 4.4 you can use a work around to achieve a darker colour:
虽然 <5.0 不支持为状态栏着色,但在 4.4 上,您可以使用变通方法来获得更深的颜色:
Make the status bar translucent
使状态栏半透明
<item name="android:windowTranslucentStatus">true</item>
Then use AppCompat's Toolbar for your appbar, making sure that it fits system windows:
然后将 AppCompat 的工具栏用于您的应用程序栏,确保它适合系统窗口:
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
...
android:fitsSystemWindows="true"/>
Make sure to set your toolbar as your activity's toolbar:
确保将工具栏设置为活动的工具栏:
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
...
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
The toolbar stretches underneath the status bar, and the semi translucency of the status bar makes it appear to be a darker secondary colour. If that's not the colour you want, this combination allows you to fit a view underneath your status bar sporting the background colour of your choice (though it's still tinted darker by the status bar).
工具栏在状态栏下方延伸,状态栏的半透明使其看起来是较暗的辅助色。如果这不是您想要的颜色,则此组合允许您在状态栏下方放置一个视图,并使用您选择的背景颜色(尽管状态栏仍将其染成更暗)。
Kind of an edge case workaround due to 4.4 only, but there ya go.
由于仅 4.4,一种边缘情况解决方法,但是你去吧。
回答by user998953
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
getWindow().setStatusBarColor(getResources().getColor(R.color.actionbar));
}
Put this code in your Activity's onCreate
method. This helped me.
将此代码放在您的活动onCreate
方法中。这对我有帮助。
回答by MehrAmoon
As others have also mentioned, this can be readily solved by adding the following to the onCreate() of the Activity:
正如其他人也提到的,这可以通过将以下内容添加到 Activity 的 onCreate() 来轻松解决:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
getWindow().setStatusBarColor(ContextCompat.getColor(this, R.color.primary_dark));
}
However, the important point I want to add here is that in some cases, even the above does not change the status bar color. For example, when using MikePenz library for Navigation Drawer, it implicityly overrides the status bar color, so that you need to manually add the following for it to work:
但是,我想在这里补充的重点是,在某些情况下,即使是上述内容也不会更改状态栏颜色。例如,在导航抽屉中使用 MikePenz 库时,它会隐式覆盖状态栏颜色,因此您需要手动添加以下内容才能使其工作:
.withStatusBarColorRes(R.color.status_bar_color)
.withStatusBarColorRes(R.color.status_bar_color)
回答by Anup Cowkur
Status bar coloring is not supported in AppCompat v7:21.0.0.
AppCompat v7:21.0.0 不支持状态栏着色。
From the Android developers blog post
On older platforms, AppCompat emulates the color theming where possible. At the moment this is limited to coloring the action bar and some widgets.
在旧平台上,AppCompat 会尽可能模拟颜色主题。目前,这仅限于为操作栏和一些小部件着色。
This means the AppCompat lib will only color status bars on Lollipop and above.
这意味着 AppCompat 库只会为 Lollipop 及更高版本的状态栏着色。
回答by alvarlagerlof
Switch to AppCompatActivity and add a 25 dp paddingTop on the toolbar and turn on
切换到 AppCompatActivity 并在工具栏上添加一个 25 dp paddingTop 并开启
<item name="android:windowTranslucentStatus">true</item>
Then, the will toolbar go up top the top
然后,将工具栏上升到顶部
回答by bapho
This solution sets the statusbar color of Lollipop, Kitkat and some pre Lollipop devices (Samsung and Sony). The SystemBarTintManageris managing the Kitkat devices ;)
此解决方案设置 Lollipop、Kitkat 和一些 Lollipop 前设备(三星和索尼)的状态栏颜色。所述SystemBarTintManager是管理奇巧装置)
@Override
protected void onCreate( Bundle savedInstanceState ) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
hackStatusBarColor(this, R.color.primary_dark);
}
@SuppressLint("NewApi")
@SuppressWarnings("deprecation")
public static View hackStatusBarColor( final Activity act, final int colorResID ) {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
try {
if (act.getWindow() != null) {
final ViewGroup vg = (ViewGroup) act.getWindow().getDecorView();
if (vg.getParent() == null && applyColoredStatusBar(act, colorResID)) {
final View statusBar = new View(act);
vg.post(new Runnable() {
@Override
public void run() {
int statusBarHeight = (int) Math.ceil(25 * vg.getContext().getResources().getDisplayMetrics().density);
statusBar.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, statusBarHeight));
statusBar.setBackgroundColor(act.getResources().getColor(colorResID));
statusBar.setId(13371337);
vg.addView(statusBar, 0);
}
});
return statusBar;
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
else if (act.getWindow() != null) {
act.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
act.getWindow().setStatusBarColor(act.getResources().getColor(colorResID));
}
return null;
}
private static boolean applyColoredStatusBar( Activity act, int colorResID ) {
final Window window = act.getWindow();
final int flag;
if (window != null) {
View decor = window.getDecorView();
if (decor != null) {
flag = resolveTransparentStatusBarFlag(act);
if (flag != 0) {
decor.setSystemUiVisibility(flag);
return true;
}
else if (Build.VERSION.SDK_INT == Build.VERSION_CODES.KITKAT) {
act.findViewById(android.R.id.content).setFitsSystemWindows(false);
setTranslucentStatus(window, true);
final SystemBarTintManager tintManager = new SystemBarTintManager(act);
tintManager.setStatusBarTintEnabled(true);
tintManager.setStatusBarTintColor(colorResID);
}
}
}
return false;
}
public static int resolveTransparentStatusBarFlag( Context ctx ) {
String[] libs = ctx.getPackageManager().getSystemSharedLibraryNames();
String reflect = null;
if (libs == null)
return 0;
final String SAMSUNG = "touchwiz";
final String SONY = "com.sonyericsson.navigationbar";
for (String lib : libs) {
if (lib.equals(SAMSUNG)) {
reflect = "SYSTEM_UI_FLAG_TRANSPARENT_BACKGROUND";
}
else if (lib.startsWith(SONY)) {
reflect = "SYSTEM_UI_FLAG_TRANSPARENT";
}
}
if (reflect == null)
return 0;
try {
Field field = View.class.getField(reflect);
if (field.getType() == Integer.TYPE) {
return field.getInt(null);
}
} catch (Exception e) {
}
return 0;
}
@TargetApi(Build.VERSION_CODES.KITKAT)
public static void setTranslucentStatus( Window win, boolean on ) {
WindowManager.LayoutParams winParams = win.getAttributes();
final int bits = WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS;
if (on) {
winParams.flags |= bits;
}
else {
winParams.flags &= ~bits;
}
win.setAttributes(winParams);
}
回答by kobra zlobra
**
**
Solution for naughty LOLLIPOP
顽皮棒棒糖的解决方案
**
**
hello i had rly big problem with having that darker than was my color... so this is solution to avoid that shadow behind status bar from solution with TranslucentStatus...
你好,我有一个比我的颜色更深的大问题......所以这是避免状态栏后面阴影的解决方案来自 TranslucentStatus 的解决方案......
so in all of your java activity you need this:
所以在你所有的java活动中你都需要这个:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
}
and at your styles.xml you need to set background color (this color will be your status bar color):
并且在你的styles.xml 你需要设置背景颜色(这个颜色将是你的状态栏颜色):
<style name="AppCompat" parent="Theme.AppCompat">
<item name="android:colorBackground">@color/YOUR_STATUS_BAR_COLOR</item>
</style>
and at all your layouts you need to add layout which will be your background:
并且在您的所有布局中,您需要添加布局作为您的背景:
<LinearLayout
android:background="@color/YOUR_BACKGROUND_COLOR"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="bottom"
/>
of course if you would like use @color/just name... you need to set that at colors.xml:
当然,如果你想使用@color/just name...你需要在colors.xml中设置:
<color name="YOUR_STATUS_BAR_COLOR">#cf031c</color>
<color name="YOUR_BACKGROUND_COLOR">#383838</color>
here is whole process how we did done that: Whats the right approach to create or change color of a status bar?
这是我们如何做到这一点的整个过程:创建或更改状态栏颜色的正确方法是什么?
回答by Ausama
Make Theme.AppCompat style parent
使Theme.AppCompat 样式父项
<style name="AppTheme" parent="Theme.AppCompat">
<item name="android:colorPrimary">#005555</item>
<item name="android:colorPrimaryDark">#003333</item>
</style>
And put getSupportActionBar().getThemedContext()
in onCreate()
.
并把getSupportActionBar().getThemedContext()
在onCreate()
。
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
getSupportActionBar().getThemedContext();
}
回答by Apostrofix
I know this doesn't answer the question, but with Material Design (API 21+) we can change the color of the status bar by adding this line in the theme declaration in styles.xml
:
我知道这不能回答问题,但是使用 Material Design(API 21+),我们可以通过在主题声明中添加以下行来更改状态栏的颜色styles.xml
:
<!-- MAIN THEME -->
<style name="AppTheme" parent="@android:style/Theme.Material.Light">
<item name="android:actionBarStyle">@style/actionBarCustomization</item>
<item name="android:spinnerDropDownItemStyle">@style/mySpinnerDropDownItemStyle</item>
<item name="android:spinnerItemStyle">@style/mySpinnerItemStyle</item>
<item name="android:colorButtonNormal">@color/myDarkBlue</item>
<item name="android:statusBarColor">@color/black</item>
</style>
Notice the android:statusBarColor
, where we can define the color, otherwise the default is used.
注意android:statusBarColor
,我们可以在其中定义颜色,否则使用默认值。