Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

FitsSystemWindows not working

I want to set FitsSystemWindows = true for my Toolbar or/and change notification bar color. But it does not work. What I wanna do: image

My code here: Styles

<!-- 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 - base Activity for other Activities

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

Toolbar

<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));
        }
}
like image 756
Prostoplus Avatar asked Oct 19 '22 03:10

Prostoplus


1 Answers

android:fitsSystemWindows is for making status bar and/or navigation bar transparent. I am not aware that it can be applied to Toolbar.

As for changing navigation bar's colour part, you can do it like this:

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);
}

And for Toolbar you can do it in XML by setting app:contentScrim="?attr/colorPrimary" property to CollapsingToolbarLayout, or in code:

collapsingToolbarLayout.setContentScrimColor(toolbarColor);
collapsingToolbarLayout.setExpandedTitleColor(textColor);
collapsingToolbarLayout.setCollapsedTitleTextColor(textColor);
like image 138
pegasus Avatar answered Oct 27 '22 01:10

pegasus