我正在开发一个简单的应用程序来测试材料设计。我正在使用com.android.support:appcompat-v7:21.0.0,我的活动看起来如下:
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>现在,我按照材料指南定义了我的主题:
<style name="AppTheme" parent="Theme.AppCompat.NoActionBar">
<item name="colorPrimary">@color/colorPrimary500</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark700</item>
</style>我想更改前安卓5中的状态栏颜色,并将其设置为colorPrimaryDark,但我找不到方法。我试着用:
getWindow().setStatusBarColor(..)但是setStatusBar颜色可以从21层获得。为什么我在我的主题中定义了一个colorPrimaryDark并使用appcompact状态栏不改变颜色?有人能帮忙吗?
发布于 2014-10-29 05:20:26
状态栏是操作系统拥有的系统窗口。在Pre-5.0Android设备上,应用程序没有更改其颜色的权限,因此AppCompat库不能支持较早的平台版本。最好的AppCompat可以提供对应用程序中的ActionBar和其他常见UI小部件着色的支持。
发布于 2014-12-17 01:02:52
虽然不支持将状态栏着色<5.0,但在4.4上,您可以使用周围的工作来获得更深的颜色:
使状态栏半透明
<item name="android:windowTranslucentStatus">true</item>然后,将AppCompat的工具栏用于您的appbar,确保它适合系统窗口:
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
...
android:fitsSystemWindows="true"/>确保将工具栏设置为活动的工具栏:
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
...
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);工具栏延伸到状态栏下面,而状态栏的半透明使它看起来是一种较深的次要颜色。如果这不是你想要的颜色,这个组合允许你在你的状态栏下面安装一个视图,显示你所选择的背景颜色(尽管它仍然被状态栏染成了更深的颜色)。
一种边缘的情况解决办法,因为只有4.4,但你走了。
发布于 2015-05-04 06:26:40
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));
}将此代码放入活动的onCreate方法中。这对我有帮助。
https://stackoverflow.com/questions/26496411
复制相似问题