2015-10-13 106 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(); 
      } 
     });