9

以下是我的主要活动的 xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:orientation="vertical">

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


    <android.support.design.widget.BottomNavigationView
        android:id="@+id/navigation"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        android:background="@color/bottom_nav_view_color"
        app:itemIconTint="@color/bottom_nav_view_icon_tint_selector"
        app:itemTextColor="@color/bottom_nav_view_text_color"
        app:menu="@menu/navigation" />

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|end"
        android:layout_marginBottom="70dp"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:layout_marginTop="10dp"
        app:srcCompat="@drawable/ic_assist_icon" />
</android.support.design.widget.CoordinatorLayout>


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

以下是Home 片段的 xml 代码

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:card_view="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:orientation="vertical">

<android.support.design.widget.AppBarLayout
    android:id="@+id/appbar"
    android:layout_width="match_parent"
    android:layout_height="245dp"
    android:background="@android:color/transparent"
    android:fitsSystemWindows="true"
    app:layout_behavior="com.gmr.android.FixAppBarLayoutBehavior">

    <android.support.design.widget.CollapsingToolbarLayout
        android:id="@+id/collapsing"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true"
        app:contentScrim="@color/card_dark_text"
        app:layout_scrollFlags="scroll|exitUntilCollapsed"
        app:titleEnabled="false">


        <ImageView
            android:id="@+id/imgAirportBg"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:fitsSystemWindows="true"
            android:scaleType="centerCrop"
            android:src="@drawable/screen_bg"
            android:tint="#8a000000"
            android:visibility="visible"
            app:layout_collapseMode="parallax" />


        <android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:app="http://schemas.android.com/apk/res-auto"
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:elevation="2dp"
            app:contentInsetLeft="0dp"
            app:contentInsetStart="0dp"
            app:title="@string/empty"
            app:contentInsetStartWithNavigation="0dp"
            app:layout_behavior=".view.ToolbarBackgroundAlphaBehavior"
            app:layout_collapseMode="pin"
            app:logo="@drawable/toolbar_logo"
            app:navigationIcon="?attr/homeAsUpIndicator"
            app:theme="@style/TransperantToolbar" />

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


<android.support.v7.widget.RecyclerView
    android:id="@+id/home_recyclerview"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:clipToPadding="false"
    android:descendantFocusability="beforeDescendants"
    android:focusable="true"
    android:focusableInTouchMode="true"
    android:layoutAnimation="@anim/layout_animation_recycler_slide_in"
    android:paddingBottom="?attr/actionBarSize"
    app:behavior_overlapTop="40dp"
    app:layout_behavior="@string/appbar_scrolling_view_behavior" />

以下是Detail 片段的 xml 代码

<?xml version="1.0" encoding="utf-8"?>
 <android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:card_view="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:orientation="vertical">

<android.support.design.widget.AppBarLayout
    android:id="@+id/appbar"
    android:layout_width="match_parent"
    android:layout_height="245dp"
    android:background="@android:color/transparent"
    android:fitsSystemWindows="true"
    app:layout_behavior="com.gmr.android.FixAppBarLayoutBehavior">

    <android.support.design.widget.CollapsingToolbarLayout
        android:id="@+id/collapsing"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:contentScrim="@color/card_dark_text"
        app:layout_scrollFlags="scroll|exitUntilCollapsed"
        app:titleEnabled="false">


        <ImageView
            android:id="@+id/imgStoreBg"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:fitsSystemWindows="true"
            android:scaleType="centerCrop"
            android:src="@drawable/screen_bg"
            android:tint="#8a000000"
            android:visibility="visible"
            app:layout_collapseMode="parallax" />

        <android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:app="http://schemas.android.com/apk/res-auto"
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:elevation="2dp"
            app:layout_behavior=".view.ToolbarBackgroundAlphaBehavior"
            app:layout_collapseMode="pin"
            app:navigationIcon="?attr/homeAsUpIndicator"
            app:theme="@style/TransperantToolbar" />

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


<android.support.v7.widget.RecyclerView
    android:id="@+id/home_recyclerview"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:clipToPadding="false"
    android:descendantFocusability="beforeDescendants"
    android:focusable="true"
    android:focusableInTouchMode="true"
    android:layoutAnimation="@anim/layout_animation_recycler_slide_in"
    android:paddingBottom="?attr/actionBarSize"
    app:behavior_overlapTop="30dp"
    app:layout_behavior="@string/appbar_scrolling_view_behavior" />

现在,当我从Home Fragment调用Detail Fragment 时,我的工具栏与状态栏重叠。如果我从任何其他片段中调用Detail 片段,它看起来很好。尽管Home 片段Detail 片段的 xml 几乎相同,但 Home 片段中的工具栏也不与状态栏重叠。

图一为细节片段正常打开时

从主页打开详细信息片段时的图片

图一为细节片段正常打开时

图二从首页打开详情片段

最新更新: 此解决方案正在帮助我https://medium.com/google-developers/windows-insets-fragment-transitions-9024b239a436但有很多更改涉及任何其他快捷方式吗?

4

3 回答 3

3

您提到的症状与此问题中提到的症状非常相似。回顾一下我在那里提供的答案,问题是,第一次显示片段时正在调度窗口插图,但下次您进行片段事务时它们不会出现。

您必须手动要求发送窗口插图才能接收正确的系统栏填充:


    private void replaceFragment() {
      getSupportFragmentManager().beginTransaction()
          .replace(R.id.content, new DetailFragment())
          // NOTE, we are performing `commitNow()` instead of ordinary `commit()`,
          // Because we want this commit to happen sychronously/immediately
          .commitNow();

      // Ask the framework to dispatch window insets once more to the root of your view hierarchy
      ViewCompat.requestApplyInsets(findViewById(R.id.root));
    }

于 2018-03-04T16:50:15.890 回答
1

我认为你应该在你的细节片段中添加这个(试一试):

            app:contentInsetLeft="0dp"
            app:contentInsetStart="0dp"
            app:title="@string/empty"

            app:logo="@drawable/customimage"

并将一个图像设置为透明并将其添加为 customimage 以获取详细信息,这可能是一个技巧。谢谢你。

于 2018-03-08T09:18:59.897 回答
1

像这样设计XML,

<android.support.design.widget.CoordinatorLayout 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/mainLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/home_bg"
    android:fitsSystemWindows="true">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/appbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@android:color/transparent"
        android:fitsSystemWindows="true"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

        <android.support.design.widget.CollapsingToolbarLayout
            android:id="@+id/collapsing_toolbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:contentScrim="@color/white"
            app:layout_scrollFlags="scroll|exitUntilCollapsed"
            app:statusBarScrim="@color/gray">

             <ImageView
                android:id="@+id/imgStoreBg"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:fitsSystemWindows="true"
                android:scaleType="centerCrop"
                android:src="@drawable/screen_bg"
                android:tint="#8a000000"
                android:visibility="visible"
                app:layout_collapseMode="parallax" />

            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?android:attr/actionBarSize"
                android:background="@android:color/transparent"
                app:layout_collapseMode="pin"
                app:theme="@style/AppTheme"
                app:title="@string/app_name">

                <TextView
                    android:id="@+id/toolbar_title"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginRight="@dimen/_45sdp"
                    android:gravity="center"
                    android:singleLine="true"
                    android:textColor="@color/black"
                    android:textSize="14dp"/>

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

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

    <android.support.v7.widget.RecyclerView
    android:id="@+id/home_recyclerview"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:clipToPadding="false"
    android:descendantFocusability="beforeDescendants"
    android:focusable="true"
    android:focusableInTouchMode="true"
    android:layoutAnimation="@anim/layout_animation_recycler_slide_in"
    android:paddingBottom="?attr/actionBarSize"
    app:behavior_overlapTop="40dp"
    app:layout_behavior="@string/appbar_scrolling_view_behavior" />
</android.support.design.widget.CoordinatorLayout>

在您的 JAVA 文件中,

onCreate

Window window = getWindow();
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
                window.setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS, WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            }
于 2018-03-05T07:00:57.940 回答