FitsSystemWindows无法正常工作

时间:2015-09-01 19:08:03

标签: java android android-support-library android-support-design

我想为我的工具栏设置FitsSystemWindows = true或/并更改通知栏颜色。但它不起作用。 我想做什么:image

我的代码在这里: 的样式

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
    <item name="android:textColorPrimary">@color/colorWhite</item>
    <item name="windowActionBar">false</item>
    <item name="windowNoTitle">true</item>
    <item name="drawerArrowStyle">@style/DrawerArrowStyle</item>
</style>

ActivityDrawerLayout - 其他活动的基本活动

<android.support.v4.widget.DrawerLayout ...
    android:fitsSystemWindows="true"
    tools:context="com.peter.freshNews.activity.DrawerActivity">

    <android.support.design.widget.NavigationView ...
        android:background="@color/drawer_background"
        android:fitsSystemWindows="true"
        app:menu="@menu/drawer_menu" />
</android.support.v4.widget.DrawerLayout>

ActivityMain

<FrameLayout ...
android:fitsSystemWindows="true">

<android.support.design.widget.CoordinatorLayout
    android:id="@+id/coordinatorLayout"
    android:fitsSystemWindows="true"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.v4.view.ViewPager.../>

    <include layout="@layout/toolbar" />
</android.support.design.widget.CoordinatorLayout>

工具栏

<android.support.design.widget.AppBarLayout 
   android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fitsSystemWindows="true"
android:background="@android:color/transparent">

    <android.support.design.widget.CollapsingToolbarLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:collapsedViewOffset="0dp"
        app:contentScrim="?attr/colorPrimary"
        app:expandedViewOffset="0dp"
        android:fitsSystemWindows="true"
        app:layout_scrollFlags="scroll|exitUntilCollapsed">

        <ViewSwitcher
            ...
        </ViewSwitcher>

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:fitsSystemWindows="true"
            android:background="@android:color/transparent" />

        <FrameLayout
            android:id="@+id/collapsing_logo"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_gravity="center">...</FrameLayout>

        <android.support.design.widget.TabLayout.../>
    </android.support.design.widget.CollapsingToolbarLayout>

MainActivity.java

  public class MainActivity extends DrawerActivity{
@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        final View contentView = inflater.inflate(R.layout.activity_main, null, false);
        mDrawerLayout.addView(contentView, 0);
//...
 collapsingToolbarLayout = (CollapsingToolbarLayout) findViewById(R.id.collapsingToolbarLayout);
        collapsingToolbarLayout.setContentScrimColor(utils.getPageColor(0));

        appBarLayout = (AppBarLayout) findViewById(R.id.appBarLayout);
        viewPager = (ViewPager) findViewById(R.id.viewPager);

        //----------------------------------t_o_o_l_b_a_r-------------------
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        if (toolbar != null) {
            toolbar.setBackgroundResource(R.color.transparent);
            setSupportActionBar(toolbar);
        }
        //---------------------------------a_c_t_i_o_n_-_b_a_r--------------
        final ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            actionBar.setHomeButtonEnabled(true);
            actionBar.setDisplayHomeAsUpEnabled(true);
            actionBar.setBackgroundDrawable(new ColorDrawable(Color.RED));
        }
}

1 个答案:

答案 0 :(得分:3)

android:fitsSystemWindows用于使状态栏和/或导航栏透明。我不知道它可以应用于工具栏。

至于更改导航栏的颜色部分,您可以这样做:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
    // clear FLAG_TRANSLUCENT_STATUS flag:
    window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
    // add FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS flag to the window
    window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
    // finally change the color
    window.setStatusBarColor(rgbColor);
}

对于工具栏,您可以通过将app:contentScrim="?attr/colorPrimary"属性设置为CollapsingToolbarLayout或代码中的XML来执行此操作:

collapsingToolbarLayout.setContentScrimColor(toolbarColor);
collapsingToolbarLayout.setExpandedTitleColor(textColor);
collapsingToolbarLayout.setCollapsedTitleTextColor(textColor);