【问题标题】:AppBarLayout transparencyAppBarLayout 透明度
【发布时间】:2016-01-11 02:38:21
【问题描述】:

我一直在玩 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>

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

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

谢谢。

【问题讨论】:

  • 您需要向我们提供更完整的xml。了解视图的父布局可能会有所帮助。
  • @Ari 提供了完整的 XML
  • 我认为它是半透明的。
  • @Apurva 好吧,正如您在屏幕截图中看到的那样,它不是 :) 正如您所看到的,它完全覆盖了 recyclerview。
  • @Kistamushken 我也遇到了这个问题。我将Toolbar 包裹在AppBarLayout 中,我希望它们都是透明的。我不知道如何让AppBarLayout 变得透明,所以我最终删除了它,只使用了ToolbarToolbar 很容易实现透明,只需设置 android:background="@android:color/transparent"

标签: android xml material-design android-appbarlayout


【解决方案1】:

为什么会这样:

根据docsCoordinateLayout 是一种特殊的FrameaLayout,因此可以假设覆盖行为与普通FrameLayout 一样工作,对吧?但问题是,当它检测到您的 RecyclerView 具有app:layout_behavior="@string/appbar_scrolling_view_behaviorflag 时,它会做不同的事情。

当它检测到滚动行为时,我假设它执行以下操作(没有查看代码,只是假设): 它将RecyclerView 放置在AppBarLayout 的下方,但保留其完整高度。发生滚动时,它所做的只是将RecyclerView 的translationY 设置为负数,以将其向上移动滚动值。

可能的解决方案:

  1. RecyclerView 的转换设置为-appBarHeight。 这将使其显示在 appBar 下方。
  2. RecyclerView 的高度增加appBarHeight。这是为了抵消由滚动行为引起的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;
    }
});

【讨论】:

  • 听起来很合法,将尽快尝试并让您知道结果,谢谢。
  • it does things differently when it detects that your RecyclerView has theapp:layout_behavior="@string/appbar_scrolling_view_behaviorflag 这帮助我找到了答案。谢谢!
【解决方案2】:

这些简单的几行代码就可以解决问题

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();
            }
        });

【讨论】:

    猜你喜欢
    • 2019-08-18
    • 2016-04-23
    • 2019-08-04
    • 2014-01-05
    • 1970-01-01
    • 2013-06-05
    • 1970-01-01
    • 2021-02-15
    • 2020-07-12
    相关资源
    最近更新 更多