android - 是否可以更改 Android 工具栏的默认矩形形状?

标签 android android-layout android-toolbar

问题:尝试实现具有拱形底部的工具栏。滚动时的内容应该经过这个弧形空间。

  • 我唯一能想到的(以及尝试过的)就是使用具有拱形的可绘制对象来设置工具栏的背景。看起来它可以解决问题。但是,当内容向上滚动时,拱形中的空白区域仍然存在,因为工具栏保持其矩形形状。

  • 我还试图避免使用自定义形状而不是工具栏,因为此工具栏是由协调器布局控制的 Collapsibletoolbarlayout 和 Appbarlayout 的一部分。

有什么建议吗?

enter image description here

最佳答案

这可以相当容易地完成。

<android.support.design.widget.CoordinatorLayout
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    xmlns:android="http://schemas.android.com/apk/res/android">

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

        <android.support.design.widget.CollapsingToolbarLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:layout_scrollFlags="scroll|exitUntilCollapsed">

            <android.support.v7.widget.Toolbar
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:background="@color/colorPrimary"
                app:layout_collapseMode="pin"/>

            <!--Other collapsing toolbar components here-->

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

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

    <android.support.v4.widget.NestedScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">

            <!--Your content here-->

    </android.support.v4.widget.NestedScrollView>

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/curve"
        app:layout_anchor="@id/appBar"
        app:layout_anchorGravity="bottom"
        android:layout_gravity="bottom"/>

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

锚定在 AppBarLayout 底部的 FrameLayout 充当拱门的容器。我创建了一个矢量可绘制对象,用作 FrameLayout

的背景

curve.xml

<vector android:width="100dp"
    android:height="40dp"
    android:viewportHeight="50"
    android:viewportWidth="400"
    xmlns:android="http://schemas.android.com/apk/res/android">

    <path
        android:pathData="M0,0L400,0L400,50Q200,-50,0,50L0,0"
        android:fillColor="@color/colorPrimary"/>

</vector>

更改可绘制对象的 android:height 属性来控制拱门的高度

输出

enter image description here

关于android - 是否可以更改 Android 工具栏的默认矩形形状?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46717543/

相关文章:

java - 如何在android中以编程方式使用KEY_DEL仅删除带有@的特定单词,如 "Whats App"

android - 错误膨胀类 ImageView - android.view.InflateException : Binary XML file line #10: Error inflating class ImageView

android - 如何减少 android studio 中的 Apk (.apk) 大小

android - SliverAppBar 没有完全折叠短列表

android - 从私钥或 keystore 生成 CSR

android - 如何使 UC 浏览器像底部栏中的设置弹出菜单?

java - 无需使用按钮即可自动打开 Bottom Sheet

Android:使用 android.support.v7.widget.Toolbar 的 RuntimeException 和 InflateException

android - 如何使用 CollapsingToolbarLayout 只有 2 个位置,滚动结束后展开和折叠

android - 继承自 AppCompat 工具栏更改工具栏图标边距