Fra*_*e91 8 android colors material-design android-design-library navigationview
我想实现一个半透明的状态栏(使我的导航视图是背后的状态栏),但还是喜欢动态地改变我的动作条的颜色.因此,状态栏颜色需要更改为我的操作栏颜色的较暗版本.
如果我将状态栏设置为透明,正如许多来源所示,我的primary_dark颜色用作状态栏的背景.但是,由于我将在运行时更改操作栏颜色,因此primary_dark不一定是我的操作栏的深色.
如果我将状态栏设置为操作栏颜色,则透明度将消失.如果我将状态栏设置为操作栏颜色并添加透明度,状态栏看起来既不错也不正确,我的重叠导航视图仍然不是非常"透明"/"彩色".
Google Inbox有三种不同的颜色:收件箱(蓝色),延后(黄色)和完成(绿色).
我该怎么做才能实现这种行为?
Kon*_*nov 10
实际上,它实施起来相当容易.
第一步 - 改变高度Toolbar:
height以wrap_content所以对我来说它看起来像这样:
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay" />
Run Code Online (Sandbox Code Playgroud)
然后覆盖以下资源v19:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Style properties -->
....
<!-- These properties are important:-->
<item name="android:windowTranslucentStatus">true</item>
<item name="windowActionBarOverlay">false</item>
<item name="android:windowActionBarOverlay">false</item>
<item name="android:fitsSystemWindows">false</item>
</style>
</resources>
Run Code Online (Sandbox Code Playgroud)
然后,在Activity,设置padding为Toolbar:
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
toolbar.setPadding(0, getStatusBarHeight(), 0, 0);
......
......
public int getStatusBarHeight() {
int result = 0;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.KITKAT) {
int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");
if (resourceId > 0) {
result = getResources().getDimensionPixelSize(resourceId);
}
}
return result;
}
Run Code Online (Sandbox Code Playgroud)
实际上,它几乎就是这样.现在,一旦我想改变工具栏的颜色,我就是这样说的:
if (getSupportActionBar() != null) {
getSupportActionBar().setBackgroundDrawable(new ColorDrawable(Color.GREEN));
}
Run Code Online (Sandbox Code Playgroud)
活动的布局:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:openDrawer="start">
<include
layout="@layout/app_bar_main"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<android.support.design.widget.NavigationView
android:id="@+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="false"
app:headerLayout="@layout/nav_header_main"
app:menu="@menu/activity_main_drawer" />
</android.support.v4.widget.DrawerLayout>
Run Code Online (Sandbox Code Playgroud)
NB!在预Kitkat操作系统版本上,状态栏保持不变,非半透明.
我已将测试应用程序的源代码上传到我的Dropbox - 随时查看它.
我希望,这有帮助
| 归档时间: |
|
| 查看次数: |
1349 次 |
| 最近记录: |