【问题标题】:FrameLayout in CoordinatorLayout making toolbar-background transparent to whiteCoordinatorLayout 中的 FrameLayout 使工具栏背景对白色透明
【发布时间】:2017-01-31 08:57:49
【问题描述】:

下面是我的代码:-

  <android.support.design.widget.CoordinatorLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/transparent"
    android:fitsSystemWindows="true">

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

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="@android:color/transparent" />
    </android.support.design.widget.AppBarLayout>


    <FrameLayout
        android:id="@+id/content_frame"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/appbarlayout"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
    />


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

这给了我预期的透明工具栏, 如下:- 但是我的 Framelayout 在工具栏上重叠,所以我尝试了

  1. 为 Framelayout 提供上边距

  2. 使用'app:layout_behavior="@string/appbar_scrolling_view_behavior"'

这段代码有效,我的 FrameLayout 没有重叠,但我的工具栏变成了 white_color。

新的输出如下

请帮忙!是什么让我的工具栏从透明背景变成白色。

【问题讨论】:

    标签: android android-toolbar android-coordinatorlayout android-framelayout


    【解决方案1】:

    这会起作用

    • 添加RelativeLayout 并为其提供背景图片
    • 在里面你可以有AppBarLayoutFrameLayoutandroid:layout_below="@+id/appbarlayout"

    例子:

    <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:background="@android:color/transparent"
        android:fitsSystemWindows="true">
    
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@drawable/amanda">
    
            <android.support.design.widget.AppBarLayout
                android:id="@+id/appbarlayout"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@android:color/transparent"
                app:elevation="0dp">
    
                <android.support.v7.widget.Toolbar
                    android:id="@+id/toolbar"
                    android:layout_width="match_parent"
                    android:layout_height="?attr/actionBarSize"
                    android:background="@android:color/transparent" />
            </android.support.design.widget.AppBarLayout>
    
    
            <FrameLayout
                android:id="@+id/content_frame"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_below="@+id/appbarlayout"
                android:background="#332"
                android:orientation="vertical" />
        </RelativeLayout>
    </android.support.design.widget.CoordinatorLayout>
    

    【讨论】:

    • 我试过了。但它也使工具栏背景变白。
    • 好的..让我再试一次
    • @Jayshree 工具栏将是透明的 将图像添加到父相关的背景 添加颜色到您的框架布局,您将看到工具栏显示背景颜色!这就是你要找的吗?
    • 是的,当我们将背景图像添加到相对布局时,工具栏是透明的。但是在我的场景中我不需要背景图片,我已经粘贴了所有有问题的代码文件,请再次检查,并帮助我使工具栏透明。
    • 好的..我知道了..谢谢
    【解决方案2】:

    这样试试

    <?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:fitsSystemWindows="true"
        android:id="@+id/main_content"
        android:layout_height="match_parent"
        android:layout_width="match_parent">
    
        <android.support.design.widget.AppBarLayout
            android:fitsSystemWindows="true"
            android:id="@+id/appbar"
            android:layout_height="wrap_content"
            android:layout_width="match_parent"
            android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
    
            <android.support.design.widget.CollapsingToolbarLayout
                android:fitsSystemWindows="true"
                android:id="@+id/collapsing_toolbar"
                android:layout_height="256dp"
                android:layout_width="match_parent"
                app:contentScrim="?attr/colorPrimary"
                app:layout_scrollFlags="scroll|exitUntilCollapsed">
    
                <ImageView
                    android:fitsSystemWindows="true"
                    android:id="@+id/image"
                    android:layout_height="256dp"
                    android:layout_width="match_parent"
                    android:scaleType="centerCrop"
                    android:src="@drawable/robert"
                    app:layout_collapseMode="parallax" />
    
                <android.support.v7.widget.Toolbar
                    android:background="@android:color/transparent"
                    android:id="@+id/toolbar"
                    android:layout_height="?attr/actionBarSize"
                    android:layout_width="match_parent"
                    app:layout_collapseMode="pin"
                    app:popupTheme="@style/ThemeOverlay.AppCompat.Light">
    
                </android.support.v7.widget.Toolbar>
    
            </android.support.design.widget.CollapsingToolbarLayout>
    
        </android.support.design.widget.AppBarLayout>
    
        <FrameLayout
            android:id="@+id/content_frame"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_behavior="@string/appbar_scrolling_view_behavior"
            android:layout_below="@+id/appbarlayout">
        </FrameLayout>
    
    </android.support.design.widget.CoordinatorLayout>
    

    【讨论】:

    • 你真的应该添加一些解释为什么这应该起作用 - 你也可以在代码本身中添加 cmets - 在当前的形式中,它没有提供任何可以帮助社区其他人的解释了解您为解决/回答问题所做的工作。
    猜你喜欢
    • 1970-01-01
    • 2014-08-21
    • 2013-04-03
    • 1970-01-01
    • 1970-01-01
    • 2015-06-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多