【问题标题】:app:elevation="0dp" hide layout inside toolbarapp:elevation="0dp" 隐藏工具栏内的布局
【发布时间】:2018-09-18 13:06:24
【问题描述】:

我在工具栏内采用了自定义布局并根据它执行操作。 但在某些情况下,我想让工具栏透明,所以当我设置 app:elevation="0dp" 时,它会隐藏我的自定义布局,但单击该区域时仍会执行操作。

我在工具栏中使用了以下布局。

已编辑

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

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

            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent">

                <ImageView
                    android:id="@+id/iv_menu"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_centerVertical="true"
                    android:background="?attr/selectableItemBackgroundBorderless"
                    android:clickable="true"
                    android:contentDescription="@null"
                    android:padding="@dimen/activity_vertical_margin"
                    android:src="@drawable/ic_menu" />


                <FrameLayout
                    android:id="@+id/frmNotification"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_centerVertical="true"
                    android:layout_toEndOf="@+id/iv_menu"
                    android:layout_toRightOf="@+id/iv_menu"
                    android:background="?attr/selectableItemBackgroundBorderless"
                    android:clickable="true"
                    android:padding="@dimen/padding_5">

                    <ImageView
                        android:id="@+id/iv_notification"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center"
                        android:contentDescription="@null"
                        android:padding="@dimen/padding_5"
                        android:src="@drawable/ic_notification" />

                    <FrameLayout
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_gravity="right"
                        android:layout_marginTop="@dimen/padding_5"
                        android:background="@drawable/bg_bage">

                        <TextView
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_gravity="center"
                            android:text="@string/eight"
                            android:textColor="@android:color/white"
                            android:textSize="@dimen/font_8" />
                    </FrameLayout>
                </FrameLayout>


                <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_centerHorizontal="true"
                    android:layout_centerVertical="true"
                    android:layout_toRightOf="@+id/frmNotification"
                    android:contentDescription="@null"
                    android:src="@drawable/logo_header" />

                <ImageView
                    android:id="@+id/ivElasticSearch"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentRight="true"
                    android:layout_centerVertical="true"
                    android:paddingLeft="@dimen/padding_10"
                    android:paddingRight="@dimen/padding_15"
                    android:src="@drawable/ic_search" />

            </RelativeLayout>


        </android.support.v7.widget.Toolbar>

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

如何克服这个问题,因为在某些情况下我们想要透明的工具栏但里面的图标应该是显示的。

这里我附上了实际的样子。

【问题讨论】:

    标签: android toolbar android-appbarlayout appbar


    【解决方案1】:

    使用android:background="@null" 而不是app:elevation="0dp"

    <android.support.design.widget.AppBarLayout
            android:id="@+id/appBarMain"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@null">
    

    编辑

    您想要移除工具栏的阴影。为此,请仅将 app:elevation="0dp" 用于 appbar 布局。修订前的代码显示您用于工具栏和应用栏。为工具栏删除它。

    更新

    我测试了代码,下面的代码似乎可以完成工作。

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout 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:background="@drawable/testbg">
    
        <android.support.design.widget.AppBarLayout
            android:id="@+id/appBarMain"
            app:elevation="0dp"
            android:background="@null"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
    
            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:background="@null"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize">
    
                <!-- Rest of the code -->
    
            </android.support.v7.widget.Toolbar>
    
        </android.support.design.widget.AppBarLayout>
    </RelativeLayout>
    

    输出图像

    【讨论】:

    • 非常感谢@Abhi 的回复,但我的需求仍然没有满足。
    • 你想要透明的工具栏。上面的代码不能解决这个问题吗?
    • Hello@Abhi using app:elevation = "0dp" 我的工具栏将隐藏这是正确的,但我想显示已经添加到工具栏中的图标,请参阅我的 xml 代码。(隐藏工具栏 + 显示图标)
    • 你好@Abhi 你有没有在工具栏(代码区域的其余部分)内拍摄图像视图?
    • 是的,imageView 在工具栏中。您的代码未更改;仅在 AppBarLayout 和 Toolbar 中进行了更改。
    【解决方案2】:

    您对app:elevation="someValue" 的想法是错误的,它不会隐藏工具栏。属性app:elevation 用于显示您想要制作多少toolbar 或任何其他支持上述属性的view表面。将表面视为放置视图的地板,现在如果您想将一些视图(如toolbar)显示到其他视图,则使用该属性。 如果你想让它透明,可以像 this 一样。

    根据Material Design Guidelinesappbar 的高度应该是4dp。所以你不应该把它设置为0dp

    【讨论】:

    • 非常感谢@dexter 的回复,您是对的。但是当我使用 toolbar.setVisibility='invisible' 或透明主题时,我的布局也会隐藏这是主要问题。
    • 你能发布一个关于正在发生的事情的图片链接吗?还是您更新的代码?并且很抱歉引导您错误不要将可见性设置为不可见,因为您希望在工具栏上执行一些操作。我还看到您的代码提到提升到 AppBarLayoutToolbar,不要这样做只是提升到 AppBarLayout
    • 我在上面更新了代码,也使用了下面的透明方法 public void changeAppBarBackgroundTransparent() { appBarMain.setBackgroundColor(getResources().getColor(android.R.color.transparent)); // setTheme(R.style.TransparentActionBar); }
    猜你喜欢
    • 2017-06-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多