使用多个片段的协调器布局时,Android状态栏扩展到工具栏



我有一个活动,该活动是通过下面的代码和布局资源来根据用户在导航抽屉中的用户选择来显示不同片段的。当活动启动时加载的第一个片段的主要片段。在Prime片段中,工具栏的显示得很好,并且根据预期的是可折叠和扩展。但是,当我切换到另一个片段时,我会遇到两个不同的问题:

  1. 状态栏扩展到工具栏。
  2. 如果在切换到另一个片段之前折叠的扩展工具栏,则汉堡图标和溢出菜单按钮不再出现。

public class MainActivity extends AppCompatActivity
    implements NavigationView.OnNavigationItemSelectedListener {
private int activeNavItemId;
private NavigationView navigationView;
private CollapsingToolbarLayout collapsingToolbarLayout;
private AppBarLayout appBarLayout;
private Toolbar toolbar;
private Resources resources;
private boolean appBarExpanded;
private FragmentManager fragmentManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    //The application initially starts with the news fragment which has the       appbar expanded.
    appBarExpanded = true;
    activeNavItemId = -1;
    toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    appBarLayout = (AppBarLayout) findViewById(R.id.app_bar_layout);
    collapsingToolbarLayout = (CollapsingToolbarLayout)    findViewById(R.id.collapsing_toolbar_layout);
    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
            this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
    drawer.setDrawerListener(toggle);
    toggle.syncState();
    resources = getResources();
    fragmentManager = getSupportFragmentManager();
    navigationView = (NavigationView) findViewById(R.id.nav_view);
    navigationView.setNavigationItemSelectedListener(this);
}
@Override
protected void onStart() {
    super.onStart();
    MenuItem primeMenuItem = navigationView.getMenu().findItem(R.id.prime);
    primeMenuItem.setChecked(true);
    onNavigationItemSelected(primeMenuItem);
}
@Override
public void onBackPressed() {
    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    if (drawer.isDrawerOpen(GravityCompat.START)) {
        drawer.closeDrawer(GravityCompat.START);
    } else {
        super.onBackPressed();
    }
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.timeline, 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) {
        Intent settingsIntent = new Intent(this, SettingsActivity.class);
        startActivity(settingsIntent);
        return true;
    }
    return super.onOptionsItemSelected(item);
}
@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
    // Handle navigation view item clicks here.
    int id = item.getItemId();
    //By default the app bar is not expandable
    boolean expandable = false;
    Class fragmentClass = null;
    if (activeNavItemId != -1) {
        navigationView.getMenu().findItem(activeNavItemId).setEnabled(true);
    }
    int title = -1;

 if (id == R.id.prime) {
        //Make the app bar expandable when on in the news fragment.
        expandable = true;
        fragmentClass = FragmentPrime.class;
        title = R.string.prime;
    } else if (id == R.id.frag_1) {
        fragmentClass = FragmentHypo.class;
        title = R.string.hypo;
    } else if (id == R.id.frag_2) {
        fragmentClass = FragmentDypo.class;
        title = R.string.dypo;
    } else if (id == R.id.frag_3) {
        fragmentClass = FragmentOz.class;
        title = R.string.oz;
    } else if (id == R.id.frag_4) {
        fragmentClass = FragmentBumbleBee.class;
        title = R.string.bumble_bee;
    }
    try {
        Fragment fragment = (Fragment) fragmentClass.newInstance();
        fragmentManager.beginTransaction().replace(R.id.fragment_container, fragment).commit();
        activeNavItemId = id;
        item.setEnabled(false);
        setAppBarExpandable(expandable);
        collapsingToolbarLayout.setTitle(resources.getString(title));
    } catch (InstantiationException e) {
        e.printStackTrace();
    } catch (IllegalAccessException e) {
        e.printStackTrace();
    }

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    drawer.closeDrawer(GravityCompat.START);
    return true;
}
private void setAppBarExpandable(boolean expandable) {
    if (expandable && !appBarExpanded) {
        appBarLayout.setFitsSystemWindows(true);
        appBarLayout.setExpanded(true, true);
        toolbar.setFitsSystemWindows(false);
        CoordinatorLayout.LayoutParams lp = (CoordinatorLayout.LayoutParams) appBarLayout.getLayoutParams();
        lp.height = (int) getResources().getDimension(R.dimen.toolbar_expand_height);
    } else if (!expandable && appBarExpanded) {
        appBarLayout.setFitsSystemWindows(false);
        collapsingToolbarLayout.setFitsSystemWindows(false);
        toolbar.setFitsSystemWindows(true);
        appBarLayout.setExpanded(false, false);
        CoordinatorLayout.LayoutParams lp = (CoordinatorLayout.LayoutParams) appBarLayout.getLayoutParams();
        lp.height = (int) getResources().getDimension(R.dimen.toolbar_height);
//            collapsingToolbarLayout.invalidate();
    }
    appBarExpanded = expandable;
}

}

----------------------------------------------------------------------------------

<include
    layout="@layout/app_bar_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />
<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:fitsSystemWindows="true"
    app:headerLayout="@layout/nav_header_main"
    app:menu="@menu/activity_main_drawer" />

------------------------------------------------------------------------------------------------

<android.support.design.widget.AppBarLayout
    android:id="@+id/app_bar_layout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:fitsSystemWindows="true"
    android:theme="@style/AppTheme.AppBarOverlay">
    <android.support.design.widget.CollapsingToolbarLayout
        android:id="@+id/collapsing_toolbar_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true"
        app:contentScrim="?attr/colorPrimary"
        app:expandedTitleMarginEnd="64dp"
        app:expandedTitleMarginStart="48dp"
        app:layout_scrollFlags="scroll|exitUntilCollapsed">
        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            android:gravity="top"
            app:layout_collapseMode="pin"
            app:popupTheme="@style/AppTheme.PopupOverlay" />
        <ImageView
            android:id="@+id/collapsing_header"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:fitsSystemWindows="true"
            android:scaleType="centerCrop"
            android:src="@mipmap/ic_launcher"
            app:layout_collapseMode="parallax" />
    </android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<!--<include layout="@layout/content_main" />-->
<FrameLayout
    android:id="@+id/fragment_container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"></FrameLayout>

styles.xml:

<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Base.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>
</style>
<style name="AppTheme.NoActionBar">
    <item name="windowActionBar">false</item>
    <item name="windowNoTitle">true</item>
</style>
<style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />
<style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />
<style name="ParallaxTabTextAppearance" parent="TextAppearance.Design.Tab">
    <item name="android:textSize">26sp</item>
    <item name="tabPaddingStart">16dp</item>
    <item name="tabPaddingEnd">16dp</item>
    <item name="textAllCaps">false</item>
    <!--<item name="android:fontFamily">sans-serif-light</item>-->
</style>
<style name="ParallaxTabLayout" parent="Widget.Design.TabLayout">
    <item name="tabBackground">@android:color/transparent</item>
    <item name="tabTextAppearance">@style/ParallaxTabTextAppearance</item>
    <item name="tabIndicatorColor">@android:color/transparent</item>
</style>

在您的清单活动中放这个

    <activity
        android:name=".circle.Group_Info"
        android:screenOrientation="portrait"
        android:theme="@style/Base.Theme.DesignDemo"
        android:windowSoftInputMode="stateAlwaysHidden"
        />

在您的样式中,如果您使用的是AppBarlayout:

    <style name="Base.Theme.DesignDemo" parent="Theme.AppCompat.DayNight.NoActionBar">
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
</style>

最新更新