AppBarLayout透明度

Kis*_*ken 2 xml android material-design android-appbarlayout

我一直在玩AppBarLayout,但我找不到让它的背景透明的方法.

这就是我的意思:

在此输入图像描述

这是我的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>
Run Code Online (Sandbox Code Playgroud)

正如你所看到我使用的颜色#8000CDFE应该给我50%的透明度,但由于某种原因它没有.我已经尝试将透明背景设置为AppBarLayout,但它也没有多大帮助.

有人遇到过这个问题吗?反正有没有为AppBarLayout及其兄弟姐妹分配透明色?

谢谢.

Ari*_*Ari 5

为什么会这样:

根据文档,CoordinateLayout是一种特殊的类型FrameaLayout,所以可以假设叠加行为像常规一样工作FrameLayout,对吧?但事实是,当它检测到您的RecyclerView具有app:layout_behavior="@string/appbar_scrolling_view_behavior标志时,它会以不同的方式执行操作.

当它检测到滚动行为时,我假设它执行以下操作(没有查看代码,只是假设):它放置在RecyclerView正下方AppBarLayout,但保持其全高.当滚动发生时,它所做的就是将RecyclerView'translationY' 设置为负数,以便通过滚动值将其向上移动.

可能的方法:

  1. 设置RecyclerView-appBarHeight的转换.这将使它出现在appBar下面.
  2. RecyclerView通过appBarHeight 增加高度.这是为了抵消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;
    }
});
Run Code Online (Sandbox Code Playgroud)