实施Nav Drawer后,通知栏为灰色 [英] Notification Bar is grey after implementing Nav Drawer

查看:83
本文介绍了实施Nav Drawer后,通知栏为灰色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试学习Android中导航抽屉的实现.

I am trying to learn implementations of Navigation Drawer in Android.

在一项活动中,我使导航抽屉位于状态栏(透明)下方和应用栏上方,并且一切正常.(左屏幕截图)

In one activity, i have made the Navigation Drawer come under the Status Bar(transparent) and over the App Bar and everything works fine.(left Screenshot)

在同一应用程序中的另一个活动中,我试图创建在应用程序栏下方拉起的导航抽屉.但是在这里,状态栏由于某种原因变成灰色(无论导航抽屉是打开还是关闭)(右侧屏幕截图).除此之外,一切似乎都很好.

In another activity in the same App, i am trying to create Navigation Drawer that pulls up under the App Bar. But here, the status bar turns grey for some reason(whether nav drawer is open or closed)(Right Screenshot). Other than this, everything seems fine.

下面是屏幕截图:

绿色导航抽屉是一个片段.

The green Nav Drawer is a fragment.

我想知道如何使状态变为正常(较暗的阴影.请记住,如果我单击应用程序栏中的双箭头图标,它将带我进入另一个活动,其中包含另一个导航抽屉,其工作原理类似于材质设计中的一个(全屏高度,并在透明的状态栏下方).这显示在屏幕快照的左侧.

What i want to know is how to make the status normal(darker shade of. Please remember, if i click that twin arrow icon in the App Bar, it will take me to another activity which contains another Nav Drawer which works like the one in Material Design(Full height of the screen and comes under a transparent Status Bar). This is shown in the left side of the screenshot.

下面是代码:

MainActivity.java:

public class MainActivity extends ActionBarActivity {
    Toolbar toolbar;
    DrawerLayout xDrawerLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_alt);

        toolbar = (Toolbar) findViewById(R.id.app_bar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayShowHomeEnabled(true);

        xDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
        xDrawerLayout.setStatusBarBackgroundColor(getResources().getColor(R.color.primary_dark));

        NavDrawerFragment mfragment = (NavDrawerFragment) getFragmentManager().findFragmentById(R.id.nav_drawer_fragment);
        mfragment.SetUp(xDrawerLayout, toolbar, R.id.nav_drawer_fragment);
    }
...

activity_alt.xml:

<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"
    android:fitsSystemWindows="true"
    android:orientation="vertical">

    <include
        android:id="@+id/app_bar"
        layout="@layout/app_bar" />


    <android.support.v4.widget.DrawerLayout
        android:id="@+id/drawer_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/hello_world" />

        </RelativeLayout>

        <fragment
            android:id="@+id/nav_drawer_fragment"
            android:name="com.rt.droid.mattest.NavDrawerFragment"
            android:layout_width="@dimen/nav_drawer_width"
            android:layout_height="match_parent"
            android:layout_gravity="start"
            tools:layout="@layout/fragment_nav_drawer" />


    </android.support.v4.widget.DrawerLayout>

</LinearLayout>

NavDrawerFragment.java:

public class NavDrawerFragment extends Fragment {

    private ActionBarDrawerToggle mDrawerToggle;
    private DrawerLayout mDrawerLayout;
    public DrawerLearner yDrawerLearner;

    public NavDrawerFragment() {
        // Required empty public constructor
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View fView = inflater.inflate(R.layout.fragment_nav_drawer, container, false);
        //fView.setFitsSystemWindows(true);
        fView.setBackgroundColor(getResources().getColor(R.color.accent));
        return fView;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    public void SetUp(DrawerLayout drawerLayout, Toolbar toolbar, int frag_id) {
        this.mDrawerLayout = drawerLayout;
        View drawerFrag = getActivity().findViewById(frag_id);
        mDrawerToggle = new ActionBarDrawerToggle(getActivity(), mDrawerLayout, toolbar, R.string.drawer_open, R.string.drawer_close) {
            @Override
            public void onDrawerOpened(View drawerView) {
                super.onDrawerOpened(drawerView);
                getActivity().invalidateOptionsMenu();
            }

            @Override
            public void onDrawerClosed(View drawerView) {
                super.onDrawerClosed(drawerView);
                getActivity().invalidateOptionsMenu();
            }
        };

        yDrawerLearner = new DrawerLearner(mDrawerLayout, drawerFrag, getActivity());
        yDrawerLearner.execute();

        this.mDrawerLayout.setDrawerListener(mDrawerToggle);
        mDrawerLayout.setStatusBarBackgroundColor(getResources().getColor(R.color.primary_dark));

        mDrawerLayout.post(new Runnable() {
            @Override
            public void run() {
                mDrawerToggle.syncState();
            }
        });

    }
}

fragment_nav_drawer.xml:

fragment_nav_drawer.xml:

<FrameLayout 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="com.rt.droid.mattest.NavDrawerFragment">

    <!-- TODO: Update blank fragment layout -->
    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="@string/hello_blank_fragment" />

</FrameLayout>

app_bar.xml:

<android.support.v7.widget.Toolbar 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="wrap_content"
    android:background="@color/primary"
    app:popupTheme="@style/AppTheme.PopupMenu"
    android:theme="@style/AppTheme.Toolbar">

</android.support.v7.widget.Toolbar>

styles.xml:

<resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="AppTheme.Base">
        <!-- Customize your theme here. -->
    </style>

    <style name="AppTheme.Base" parent="Theme.AppCompat.Light.NoActionBar">
        <item name="colorPrimary">@color/primary</item>
        <item name="colorPrimaryDark">@color/primary_dark</item>
        <item name="colorAccent">@color/accent</item>
    </style>

    <style name="AppTheme.Toolbar" parent="ThemeOverlay.AppCompat.Dark">
        <item name="android:textColorPrimary">@color/primary_text</item>
        <item name="android:textColorSecondary">@color/accent</item>
    </style>

    <style name="AppTheme.PopupMenu" parent="ThemeOverlay.AppCompat.Dark">
        <item name="android:background">@color/accent</item>
    </style>

</resources>

styles.xml(v21):

<resources>
    <style name="AppTheme" parent="AppTheme.Base">
        <!-- Customize your theme here. -->
        <item name="android:colorPrimary">@color/primary</item>
        <item name="android:colorPrimaryDark">@color/primary_dark</item>
        <item name="android:colorAccent">@color/accent</item>
        <item name="android:windowDrawsSystemBarBackgrounds">true</item>
        <item name="android:statusBarColor">@android:color/transparent</item>
        <item name="android:windowTranslucentStatus">true</item>
    </style>

</resources>

Colors.xml(显示颜色样本的屏幕截图):

注意:我不想在样式等方面做出妥协,而这会使我的其他导航抽屉无法正常工作.换句话说,我更喜欢这样一种解决方案,其中两种类型的导航栏都可以在同一应用程序中正常工作.

Note: I do not want to compromise on the styles etc that would render my other navigation drawer not working. In other words, i prefer a solution wherein both types of navigation bar works as expected in the same app.

如果您需要任何信息,请告诉我,如有需要,我将进行编辑. 添加了app_bar.xml&为了清晰起见,使用colors.xml.

Please let me know if you need any info and i shall edit if required. added app_bar.xml & colors.xml for clarity.

推荐答案

但是在这里,状态栏由于某种原因变成灰色

But here, the status bar turns grey for some reason

这是状态栏背景下的活动白色背景上的半透明(25%黑色).

It's the translucent (25% black) status bar background over the white background of activity underneath it.

我想知道的是如何使状态正常

What i want to know is how to make the status normal

您需要为第二项活动禁用半透明状态栏.在您的主题定义中此行将其激活:

You need to disable translucent status bar for the second activity. It is activated by this line in your theme definition:

<item name="android:windowTranslucentStatus">true</item>
<item name="android:statusBarColor">@color/primary_dark</item>

因此,可以定义一个子主题并用false覆盖该标志,或者通过从您的活动中调用该标志来以编程方式清除该标志:

So either define a child theme and override the flag with false or clear the flag programmatically by calling this from your activity:

if (Build.VERSION.SDK_INT >= 21) {
    getWindow().clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
    getWindow().setStatusBarColor(getResources().getColor(R.color.primary_dark);
}

这篇关于实施Nav Drawer后,通知栏为灰色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆