Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/218.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 为什么应用程序栏会在我的应用程序栏的背景中添加方块_Android_Xml_Layout_Toolbar_Appbar - Fatal编程技术网

Android 为什么应用程序栏会在我的应用程序栏的背景中添加方块

Android 为什么应用程序栏会在我的应用程序栏的背景中添加方块,android,xml,layout,toolbar,appbar,Android,Xml,Layout,Toolbar,Appbar,大家下午好 我已经在这方面挖了很长时间了,现在是寻求帮助的时候了 我有下面的AppBar <?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app

大家下午好

我已经在这方面挖了很长时间了,现在是寻求帮助的时候了

我有下面的AppBar

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    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:id="@+id/app_bar_constrained"
    android:layout_height="?android:attr/actionBarSize"
    android:layout_width="match_parent"
    android:background="@drawable/appbar_background_2020">

    <com.google.android.material.appbar.AppBarLayout
        android:id="@+id/app_bar"
        android:elevation="0dp"
        android:background="@drawable/appbar_background_2020"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintTop_toTopOf="@id/app_bar_constrained">

        <com.google.android.material.appbar.CollapsingToolbarLayout
            android:id="@+id/app_bar_collapsing"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:elevation="0dp">

            <androidx.appcompat.widget.Toolbar
                android:id="@+id/toolbar_2020"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:elevation="0dp">

                <LinearLayout
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:orientation="vertical">

                    <TextView
                        android:id="@+id/toolbar_title"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        tools:text="Title" />

                    <TextView
                        android:id="@+id/toolbar_sub_title"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:visibility="gone"
                        tools:text="Subtitle"
                        tools:visibility="visible" />
                </LinearLayout>
            </androidx.appcompat.widget.Toolbar>

        </com.google.android.material.appbar.CollapsingToolbarLayout>

        <com.google.android.material.tabs.TabLayout
            android:id="@+id/tab_bar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:fadingEdge="horizontal"
            android:fadingEdgeLength="24dp"
            android:visibility="gone"
            android:layout_marginStart="24dp"
            android:layout_marginEnd="@24dp"
            android:elevation="0dp"
            android:background="@drawable/appbar_background_2020"
            app:tabBackground="?attr/selectableItemBackground"
            app:tabGravity="start"
            app:tabIndicator="@drawable/tab_indicator"
            app:tabIndicatorColor="@color/colorPrimary"
            app:tabIndicatorHeight="1dp"
            app:tabMode="scrollable"
            app:tabPaddingEnd="2dp"
            app:tabPaddingStart="2dp"
            app:tabRippleColor="@color/colorAccent"
            app:tabSelectedTextColor="@color/colorAccent"
            app:tabTextAppearance="@style/Widget.AppCompat.Light.ActionBar.TabBar"
            app:tabTextColor="@color/colorPrimary"
            tools:layout_conversion_absoluteHeight="48dp"
            tools:layout_conversion_absoluteWidth="0dp" />

    </com.google.android.material.appbar.AppBarLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

我以前通过使用ConstraintLayout而不是AppBar来解决这个问题,但这不适用于CoordinatorLayout的动画,我们非常希望使用CoordinatorLayout

请注意:由于公司的考虑,我不得不更改上述代码示例中的颜色

编辑:在回答以下问题时,背景文件是

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">

<corners
    android:bottomLeftRadius="24dp"
    android:bottomRightRadius="24dp"
    android:topLeftRadius="0dp"
    android:topRightRadius="0dp" />

<solid android:color="@color/white" />

问题是因为您在AppBarLayout中使用的是android:elevation=“0dp”而不是app:elevation=“0dp”

在活动中还添加以下代码:

findViewById(R.id.campfire_app_bar).bringToFront(); 

您只能使用
LinearLayout
,而不是使用
AppBar
。我希望这能解决你的问题

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    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:id="@+id/app_bar_constrained"
    android:layout_height="?android:attr/actionBarSize"
    android:layout_width="match_parent">

    <LinearLayout
        android:id="@+id/app_bar"
        android:elevation="0dp"
        android:background="@drawable/appbar_background_2020"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintTop_toTopOf="@id/app_bar_constrained"
        android:orientation="horizontal">

        <com.google.android.material.appbar.CollapsingToolbarLayout
            android:id="@+id/app_bar_collapsing"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:elevation="0dp">

            <androidx.appcompat.widget.Toolbar
                android:id="@+id/toolbar_2020"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:elevation="0dp">

                <LinearLayout
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:orientation="vertical">

                    <TextView
                        android:id="@+id/toolbar_title"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        tools:text="Title" />

                    <TextView
                        android:id="@+id/toolbar_sub_title"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:visibility="gone"
                        tools:text="Subtitle"
                        tools:visibility="visible" />
                </LinearLayout>
            </androidx.appcompat.widget.Toolbar>

        </com.google.android.material.appbar.CollapsingToolbarLayout>

        <com.google.android.material.tabs.TabLayout
            android:id="@+id/tab_bar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:fadingEdge="horizontal"
            android:fadingEdgeLength="24dp"
            android:visibility="gone"
            android:layout_marginStart="24dp"
            android:layout_marginEnd="24dp"
            android:elevation="0dp"
            android:background="@drawable/appbar_background_2020"
            app:tabBackground="?attr/selectableItemBackground"
            app:tabGravity="start"
            app:tabIndicator="@drawable/tab_indicator"
            app:tabIndicatorColor="@color/colorPrimary"
            app:tabIndicatorHeight="1dp"
            app:tabMode="scrollable"
            app:tabPaddingEnd="2dp"
            app:tabPaddingStart="2dp"
            app:tabRippleColor="@color/colorAccent"
            app:tabSelectedTextColor="@color/colorAccent"
            app:tabTextAppearance="@style/Widget.AppCompat.Light.ActionBar.TabBar"
            app:tabTextColor="@color/colorPrimary"
            tools:layout_conversion_absoluteHeight="48dp"
            tools:layout_conversion_absoluteWidth="0dp" />

    </LinearLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

由于AppBarLayout阴影的高度,这些工件来自AppBarLayout阴影。(您的布局将所有立面设置为
0dp
,但这可能不是您实际使用的。)

如果需要立面但不需要阴影,则可以为AppBarLayout指定以下内容:

另一件可能会让人困惑的事情是:您过度使用了可绘制的
appbar\u background\u 2020
。我认为你应该找到最好的地方,只指定一次


上面的方法可以消除阴影,但是,如果您想要沿着AppBarLayout曲线的阴影,您可以通过在线搜索解决这一问题。

您可以尝试将标高设置为0?是的,我已经尝试过了。问题似乎来自AppBarLayout——如果我删除它,我可以去掉那些小角落。你可以尝试将背景形状设置为工具栏,并使AppBarLayout透明或不可见?没有骰子。将可见性设置为“不可见”会隐藏所有组件,而将颜色设置为“透明”不会产生任何影响。您可以添加什么问题吗?当我将android:elevation更改为
app:elevation
时,z顺序将被删除(以及阴影伪影),指向下面的卡片,在工具栏顶部滚动。通过在appBar上添加bringToFront()并结合app:elevation=“0dp”检查我的更新答案。如果问题仍然存在,请在github上上载一个演示项目,以复制该问题。@skooter请将您的完整代码与scrollbar共享,包括CardsHanks@MariosP-这很有效。下一步,我要想办法创建我自己的阴影。谢谢@Cheticamp。请详细介绍一下关于背景绘图的评论好吗?
outlineProvider
解决方案不适用于
AppBarLayout
@skooter您在三个地方使用背景绘图。我想知道这是否正确。我还以为它是在工具栏上定义的,但它不是,所以它很可能不是问题。其他东西对我有用,但现在不行,所以不确定那里发生了什么。我确实发现,将AppBar的
app:elevation
设置为任何值都可以消除瑕疵,因此您可以指定
app:elevation=“4dp”
来设置默认的高度并消除瑕疵。我使用的是材料1.2.1。
android:outlineProvider="none" // Eliminates the shadow for API 21+. 
                               // Below API 21, there shouldn't be a shadow
android:elevation="2dp"        // Set elevation for API 21+