android - 如何让新的 NavigationView 与状态栏纱幕搭配得很好?

标签 android material-design android-design-library androiddesignsupport

我一直在使用 Google 的新设计支持库,非常棒!我只是在导航 View 上有点难过。我读到的所有东西都说 NavigationView 足够聪明,可以自己处理透明的稀松布。 (android-developers 帖子,一个;搜索 scrim)。无论如何,当我尝试这样做时,我得到以下结果:

Transparent Scrim

这很棒;正是我想要的。除了一件事。当抽屉关闭时,稀松布是难看的深灰色,而不是我的 primaryColorDark 。 . .

enter image description here

这是我的布局:

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true" >

<android.support.design.widget.CoordinatorLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true">

    <LinearLayout
        android:id="@+id/linear_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <include layout="@layout/toolbar" />

        <fragment
            android:id="@+id/fragment"
            android:name="com.gmail.rixx.justin.envelopebudget.HomeFragment"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            tools:layout="@layout/fragment_home" />

    </LinearLayout>

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:layout_gravity="end|bottom"
        android:layout_margin="@dimen/fab_margin"
        app:backgroundTint="@color/accent"
        android:src="@drawable/ic_add_white_24dp" />

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

<android.support.design.widget.NavigationView
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    app:headerLayout="@layout/drawer_header"
    app:menu="@menu/drawer" />

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

Activity 代码:

public class Home extends ActionBarActivity {

    private Toolbar mToolbar;
    private DrawerLayout mDrawerLayout;
    private Context mContext = this;

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

        setUpToolbar();
        setUpNavDrawer();

        findViewById(R.id.fab).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(mContext, NewTransactionActivity.class));
            }
        });
    }

    private void setUpToolbar() {
        mToolbar = (Toolbar) findViewById(R.id.toolbar);
        if (mToolbar != null) {
            setSupportActionBar(mToolbar);
        }
    }

    private void setUpNavDrawer() {
        if (mToolbar != null) {
            mDrawerLayout = (DrawerLayout)     findViewById(R.id.drawer_layout);

            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
            mToolbar.setNavigationIcon(R.drawable.ic_menu_white_24dp);
            mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mDrawerLayout.openDrawer(GravityCompat.START);
                }
            });
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_home, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

以及 v21/样式:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="AppTheme" parent="AppTheme.Base">
        <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:textColorPrimary">@color/primary_text</item>
        <item name="android:textColorSecondary">@color/secondary_text</item>

        <item name="android:windowDrawsSystemBarBackgrounds">true</item>
        <item name="android:statusBarColor">@android:color/transparent</item>

    </style>
</resources>

我模仿了 github 上 Chris Banes 的 CheeseSquare 应用程序但我没有得到我想要的行为。

我尝试从 v21/styles 中删除 windowDrawsSystemBarBackgroundsstatusBarColor,我得到了正确的颜色,但状态栏永远不会透明:

enter image description here

我们将不胜感激。这是新东西,所以我知道我们都在学习。

感谢阅读!

-贾斯汀

最佳答案

在为此苦苦挣扎了几个小时,并将我的代码与 cheesesquare 应用程序进行了大量比较之后,我发现了以下内容: DrawerLayout 必须具有属性 android:fitsSystemWindows="true"NavigationView 也是如此,但 CoordinatorLayout 不应该。一旦我进行了这些更改,它就起作用了。

谢谢大家,希望这对某人有帮助!

您可以查看我的布局代码 here .

-贾斯汀

关于android - 如何让新的 NavigationView 与状态栏纱幕搭配得很好?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30928296/

相关文章:

android - 运行多个测试时,只有第一个测试通过 TestScheduler (Kotlin)

android - 在 Fragments 和 Show 之间切换隐藏 AlertDialog 并保持状态

android - 在 android 中的谷歌地图上设置缩放级别

python - Django 表单和 Material Design Lite

java - AndroidcardView点击回调

android - 透明的AppBarLayout,其中包含CardView

android - 如何识别用户是否在 Android 上的权限对话框之外单击

android - Android Material Design 中的 float 操作按钮是否有原生组件?

android - API 16 设备上的 fitsSystemWindows 不工作

android - 带有支持库的 FloatingActionButton 示例