动态更改状态栏的背景,同时保持透明度

时间:2015-11-17 16:21:54

标签: android android-navigation-drawer android-statusbar android-navigationview

上下文

我有一个基本活动,其中包含来自android studio中示例的drawer navigation。我让statusbar透明了,我希望菜单在它下滑动,允许它的颜色溢出到透明度中,如下所示:

(注意右边的蓝色)

enter image description here

但默认情况下,它看起来像这样:

Android navigation drawer

问题

蓝色来自主题colorPrimaryDark,但我想动态改变这种颜色,所以我不能依赖主题。有没有办法动态/编程设置颜色?

我尝试的事情

  • 调整装饰视图的颜色; getWindow().getDecorView().setBackgroundColor(Color.White)
  • 设置状态栏的颜色并使用标志 DRAWS_SYSTEM_BAR_BACKGROUNDS

    if(android.os.Build.VERSION.SDK_INT> = Build.VERSION_CODES.LOLLIPOP){ window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS); window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS); window.setStatusBarColor(Color.BLUE); }

(对不起编码块不好,它不想听)

这不会使其透明为第一张图片,而只是纯色。

  • 更改rootView anyView.getRootView().setBackgroundColor()
  • 的颜色

XML文件:root_controller.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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">

    <android.support.v4.widget.DrawerLayout
        android:id="@+id/drawer_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true"
        android:clipToPadding="false"
        tools:openDrawer="end">


        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">

            <!-- toolbar -->
            <include android:id="@+id/toolbar_group"
                layout="@layout/toolbar"/>

            <FrameLayout
                android:id="@+id/main_content"
                android:layout_width="match_parent"
                android:layout_height="match_parent"/>

        </LinearLayout>

        <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.support.v4.widget.DrawerLayout>


</RelativeLayout>

XML文件工具栏:

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.AppBarLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="52dp">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="52dp"
        app:layout_scrollFlags="scroll|enterAlways" />

</android.support.design.widget.AppBarLayout>

RootController活动

public class RootController extends AppCompatActivity implements
        NavigationView.OnNavigationItemSelectedListener {

    @Override
    public void setContentView(int layoutResID) {
        rootView = getLayoutInflater().inflate(R.layout.root_controller, null);
        drawer = (DrawerLayout)rootView.findViewById(R.id.drawer_layout);
        FrameLayout frameLayout = (FrameLayout) drawer.findViewById(R.id.main_content);

        targetView = getLayoutInflater().inflate(layoutResID, frameLayout, true);

        initMenu(drawer);
        super.setContentView(rootView);
    }

private void initMenu(DrawerLayout drawer){

        // get toolbar
        toolbarGroup = rootView.findViewById(R.id.toolbar_group);
        initToolbarGroup(toolbarGroup, "test");



        // get the navigation view
        nav = (NavigationView) drawer.findViewById(R.id.nav_view);
        nav.setBackgroundColor(ColorDelegate.getNavColor());
        Menu menu = nav.getMenu();

        // set navigation listener
        nav.setNavigationItemSelectedListener(this);

        // ... fill menu
    }

1 个答案:

答案 0 :(得分:0)

Activity

中尝试使用此功能
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
    Window window = getWindow();
    String dynamicColor = "#99000000";
    window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
    window.addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN); /*You were missing this*/
    window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
    window.setStatusBarColor(Color.parseColor(dynamicColor));
}