9

我正在使用新設計庫開發Android應用程序。我想創建一個類似的滾動效果,用於新的Google Photos app。我希望AppBarLayout完全滾動屏幕,以便回收站視圖在狀態欄後面滾動。如何使AppBarLayout完全消失,並將windowTranslucentStatus設置爲true

我已經在應用程序的主題中將windowTranslucentStatus設置爲true。這裏的主要活動的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" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <android.support.v7.widget.RecyclerView 
     android:id="@+id/recyclerView" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     app:layout_behavior="@string/appbar_scrolling_view_behavior" /> 

    <android.support.design.widget.AppBarLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content"> 

     <android.support.v7.widget.Toolbar 
      android:id="@+id/toolbar" 
      android:layout_width="match_parent" 
      android:layout_height="?attr/actionBarSize" 
      app:layout_scrollFlags="scroll|enterAlways" /> 

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

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

下面是這個樣子,當向下滾動,如: Toolbar not disappearing completely

工具欄不會完全消失。

感謝您的幫助!

+1

我開始懷疑他們甚至使用了CoordinatorLayout ...我我一直被困在這個確切的問題上。 – timothyjc

+0

@timothyjc如果您在我之前找到解決方案,可以將其發佈到此處。我認爲你是對的 – alesko007

+0

上傳我的示例項目到github,以防萬一任何人想要輕鬆地看到問題... https://github.com/timothyjc/TestALot – timothyjc

回答

0

這是關於儘可能靠近我可以與CoordinatorLayout(只是工具欄上的利潤率扮演)得到......不夠緊密IMO:

<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:id="@+id/main_content" 
    android:fitsSystemWindows="true" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 


<android.support.design.widget.AppBarLayout 
    android:id="@+id/appbar" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content"> 

    <android.support.v7.widget.Toolbar 
     android:id="@+id/toolbar" 
     android:paddingTop="25dp" 
     android:layout_width="match_parent" 
     android:layout_height="81dp" 
     android:layout_marginTop="-28dp" 
     android:background="#00ffffff" 
     app:layout_scrollFlags="scroll|enterAlways" /> 

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

<android.support.v7.widget.RecyclerView 
    android:id="@+id/recyclerview" 
    android:background="#ffffff" 
    android:fitsSystemWindows="true" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    app:layout_behavior="@string/appbar_scrolling_view_behavior" /> 


<android.support.design.widget.FloatingActionButton 
    android:id="@+id/fab" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_gravity="end|bottom" 
    android:layout_margin="16dp" 
    android:src="@drawable/ic_headphones_white_24dp" /> 

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

我認爲這將是更容易做到這一點手動...

你有什麼想法或進展?

+0

我正在嘗試設置高度和邊距以協調器佈局。但是這造成了新的問題。你弄明白了嗎? – alesko007

+0

Reddit上的一些人告訴我存在一個錯誤,它將在下一個版本的支持設計庫中得到修復。我最終手動完成了所有的動畫。 – timothyjc

+0

@timothyjc fitsSystemWindows也必須位於AppBarLayout上。 And android:windowDrawsSystemBarBackgrounds = true應該存在於v-21 styles.xml中 這是我做的工作。 – Suleiman19

-1

爲您的AppBarLayout和CoordinatorLayout添加fitsSystemWindows =「true」。


+0

這不起作用 – alesko007

+0

這不會使工具欄變得透明。 –

0

在我的情況下,我加入fitSystemWindows = 「false」 表示CoordinatorLayout和fitSystemWindows = 「true」 將其子 - 你的情況給RecyclerView。在NavigationView,DrawerLayout,AppBarLayout和工具欄上它的「真實」。我嘗試過不同的價值觀,然後在我的情況下突破。 Android 5.1.1和最新的支持庫(23.0.1)。希望能幫助到你。

1

這是我在我的應用程序

<android.support.design.widget.AppBarLayout 
    android:id="@+id/app_bar" 
    android:layout_width="match_parent" 
    android:layout_height="@dimen/app_bar_height" 
    android:fitsSystemWindows="true" 
    android:theme="@style/AppTheme.AppBarOverlay"> 

    <android.support.design.widget.CollapsingToolbarLayout 
     android:id="@+id/toolbar_layout" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:fitsSystemWindows="true" 
     app:contentScrim="?attr/colorPrimary" 
     app:layout_scrollFlags="scroll|enterAlwaysCollapsed"> 

     <android.support.v7.widget.Toolbar 
      android:id="@+id/toolbar" 
      android:layout_width="match_parent" 
      android:layout_height="?attr/actionBarSize" 
      app:layout_collapseMode="pin" 
      app:popupTheme="@style/AppTheme.PopupOverlay" /> 

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

使用普通AppBarLayout當你創建Android Studio中的Android項目是來自應用程序模板模式中使用。在XML佈局和Java代碼只是一些變化:

XML佈局:

<android.support.design.widget.AppBarLayout 
    android:id="@+id/app_bar" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:theme="@style/AppTheme.AppBarOverlay" 
    > 

    <FrameLayout 
     android:id="@+id/toolbar_container" 
     android:fitsSystemWindows="true" 
     app:layout_scrollFlags="scroll|enterAlways" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:background="?attr/colorPrimary" 
     > 

     <android.support.v7.widget.Toolbar 
      android:id="@+id/toolbar" 
      android:layout_width="match_parent" 
      android:layout_height="?attr/actionBarSize" 
      app:popupTheme="@style/AppTheme.PopupOverlay" 
      /> 

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

Java代碼:

ViewCompat.setOnApplyWindowInsetsListener(toolbar, new OnApplyWindowInsetsListener() { 
     @Override 
     public WindowInsetsCompat onApplyWindowInsets(View v, WindowInsetsCompat insets) { 
      toolbarContainerLayout.setPadding(0, insets.getSystemWindowInsetTop(), 0, 0); 

      return insets; 
     } 
    }); 
相關問題