1

语境

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

(注意右边的蓝色)

在此处输入图像描述

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

Android 导航抽屉

问题

蓝色来自主题 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>

根控制器活动

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
    }
4

1 回答 1

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));
}
于 2017-01-05T07:04:18.510 回答