2015-10-13 150 views
2

我一直在玩AppBarLayout,但是我找不到讓背景透明的方法。AppBarLayout透明度

這裏我的意思:

enter image description here

這裏是我的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:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:fitsSystemWindows="true" 
    tools:context=".MainActivity"> 


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

    <android.support.design.widget.AppBarLayout 
     android:id="@+id/appbar" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     app:elevation="0dp" 
     android:background="@android:color/transparent"> 

     <TextView 
      android:id="@+id/toolbar" 
      app:elevation="0dp" 
      android:text="hey" 
      android:textColor="@android:color/white" 
      android:layout_width="match_parent" 
      android:layout_height="45dp" 
      android:background="#8000CDFE" 
      app:layout_scrollFlags="scroll|enterAlways"/> 

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

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

正如你可以看到我使用的顏色#8000CDFE應該給我50%的透明度,但由於某種原因它沒有。我已經嘗試將透明背景設置爲AppBarLayout,但它也沒有多大幫助。

有沒有人遇到過這個問題?無論如何要將透明顏色分配給AppBarLayout及其「兄弟姐妹」?

謝謝。

+0

您需要向我們提供一個更完整的XML 。知道你的意見的父母佈局可能會有所幫助。 – Ari

+0

@Ari提供了完整的XML – Kistamushken

+0

我認爲它是半透明的。 – Apurva

回答

5

爲什麼會發生:

按照docsCoordinateLayout是一種特殊的一個FrameaLayout,所以可以假定疊加的行爲,就像它的工作與常規FrameLayout,對不對?但事實是,當它檢測到您的RecyclerView具有app:layout_behavior="@string/appbar_scrolling_view_behavior標誌時,它會以不同的方式做事。

當檢測到滾動beahvior,我假設它下面(沒有看過代碼,只是假設): 它把RecyclerView略低於AppBarLayout,但保留其完整高度。滾動發生時,它所做的全部操作是將RecyclerView的翻譯Y設置爲負數,以便按滾動值向上移動它。

可能的解決辦法:

  1. RecyclerView集轉換到-appBarHeight。 這將使它出現在appBar下面。
  2. 通過appBarHeight增加RecyclerView的高度。這是爲了抵消由scrollBehavior引起的translationY變化。

例如,這裏是你如何能做到這一點:

ViewTreeObserver vto = mRecyclerView.getViewTreeObserver(); 
vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() { 
    @Override 
    public void onGlobalLayout() { 

     if (Build.VERSION.SDK_INT > Build.VERSION_CODES.ICE_CREAM_SANDWICH_MR1) { 
      mRecyclerView.getViewTreeObserver().removeOnGlobalLayoutListener(this); 
     } else { 
      mRecyclerView.getViewTreeObserver().removeGlobalOnLayoutListener(this); 
     } 

     recyclerView.setTranslationY(-appBarHeight); 
     recyclerView.getLayoutParams().height = recyclerView.getHeight()+appBarHeight; 
    } 
}); 
+0

聽起來合法,會盡快嘗試並讓您知道結果,謝謝。 – Kistamushken

0

這些簡單的幾行代碼會做的伎倆

RecyclerView mRecyclerView = (RecyclerView) findViewById(R.id.recycler); 
AppBarLayout barLayout = (AppBarLayout) findViewById(R.id.appbar);  

mRecyclerView.addOnLayoutChangeListener(new View.OnLayoutChangeListener() { 
      @Override 
      public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) { 
       mRecyclerView.setTranslationY(-barLayout.getHeight()); 
       mRecyclerView.getLayoutParams().height = mRecyclerView.getHeight()+barLayout.getHeight(); 
      } 
     });