我正在开发一个简单的应用程序来测试 Material 设计。我正在使用 com.android.support:appcompat-v7:21.0.0
我的 Activity 看起来像:
public class MyActivity extends ActionBarActivity {
...
}
布局定义为:
<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>
现在我按照 Material 指南定义了我的主题:
<style name="AppTheme" parent="Theme.AppCompat.NoActionBar">
<item name="colorPrimary">@color/colorPrimary500</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark700</item>
</style>
我想在 Android 5 之前更改状态栏颜色并将其设置为 colorPrimaryDark
但我找不到方法。我尝试使用:
getWindow().setStatusBarColor(..)
但是 setStatusBar 颜色从 21 级开始可用。
为什么如果我在我的主题中定义一个 colorPrimaryDark
并使用 appcompact 状态栏不会改变颜色?
有人可以帮忙吗?
最佳答案
虽然 <5.0 不支持为状态栏着色,但在 4.4 上,您可以使用变通方法来实现更深的颜色:
使状态栏半透明
<item name="android:windowTranslucentStatus">true</item>
然后为您的应用栏使用 AppCompat 的工具栏,确保它适合系统窗口:
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
...
android:fitsSystemWindows="true"/>
确保将您的工具栏设置为 Activity 的工具栏:
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
...
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
工具栏在状态栏下方延伸,状态栏的半透明使其看起来是一种较暗的辅助颜色。如果这不是您想要的颜色,此组合可让您在状态栏下方放置一个显示您选择的背景颜色的 View (尽管状态栏仍将其染成较暗的颜色)。
由于仅适用于 4.4,这是一种边缘情况的解决方法,但你去吧。
关于安卓 Material : Status bar color won't change,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26496411/