Android 带Appcompat的浮动工具栏

Android 带Appcompat的浮动工具栏,android,toolbar,android-ui,android-appcompat,Android,Toolbar,Android Ui,Android Appcompat,如何按照“材质设计指南”和“谷歌地图”应用程序中的建议创建一个浮动工具栏,如下图所示 我以前使用过工具栏,来自Commonware的所有评论都是绝对正确的 工具栏小部件()与任何其他视图组没有任何特殊或不同之处,其行为与任何其他视图组都没有区别 将其放入框架布局,在其上放置布局_边距参数,使布局_宽度不匹配_父项,即是如此 将其放入带有方向=水平的线性布局中,您可以使用布局重量控制百分比大小。如果适合您的需要,也可以使用普通的dip。我认为Mark关于在上面的评论中选择CardView“外观”的

如何按照“材质设计指南”和“谷歌地图”应用程序中的建议创建一个浮动工具栏,如下图所示


我以前使用过工具栏,来自Commonware的所有评论都是绝对正确的

工具栏
小部件()与任何其他
视图组
没有任何特殊或不同之处,其行为与任何其他视图组都没有区别

将其放入
框架布局
,在其上放置
布局_边距
参数,使
布局_宽度
不匹配_父项,即是如此


将其放入带有
方向=水平的
线性布局
中,您可以使用
布局重量
控制百分比大小。如果适合您的需要,也可以使用普通的
dip

我认为Mark关于在上面的评论中选择CardView“外观”的建议值得这个衍生答案:

只需将
工具栏
放入
卡片视图

<android.support.v7.widget.CardView
    android:id="@+id/map_toolbar_container"
    android:layout_width="@dimen/whatever_you_want"
    android:layout_height="wrap_content"
    android:layout_margin="8dp"
    app:cardElevation="8dp"
    app:cardBackgroundColor="#324">

    <android.support.v7.widget.Toolbar
        android:id="@+id/wld_toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="?attr/colorPrimary"
        android:minHeight="?attr/actionBarSize"/>

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

由于您遵循的是材料设计概念,我假设您使用的是主布局,而不是框架布局

在做其他事情之前,我们需要声明重要的依赖项

dependencies {
    compile 'com.android.support:design:22.2.1'
    compile 'com.android.support:cardview-v7:22.2.1'
}
预期/类似产出

XML片段


只需添加以下代码

<android.support.v7.widget.Toolbar
    android:layout_width="match_parent"
    android:layout_height="?android:attr/actionBarSize"
    android:id="@+id/toolbar"
    android:background="@color/colorPrimary"
    android:elevation="8dp"
    android:layout_margin="5dp"
    >
     //add whatever elements you want to add in here.
</android.support.v7.widget.Toolbar>

//在这里添加您想要添加的任何元素。
这是浮动工具栏的代码,只需复制并粘贴即可。 这是一个使用Appcompat的浮动工具栏示例,它可以在许多布局下工作,如相对、协调或抽屉布局。可以增加高程以使其更有效



用于设置背景阴影的可绘制/浮动工具栏.xml文件

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
    <shape
        android:shape="rectangle">
        <solid android:color="#6FA5A5A5" />
        <corners android:radius="6dp"/>
    </shape>
</item>
<item android:top="1dp" android:right="1dp" android:left="1dp" android:bottom="1dp">
    <shape
        android:shape="rectangle">
        <solid android:color="@color/colorWhite"/>
        <corners android:radius="5dp"/>
    </shape>
</item>
</layer-list>


用于EditText的drawable/edit_text_no_border.xml文件,不带边框或底部边框,以使其干净匹配背景

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <solid android:color="#ffffff"/>
    <corners  android:radius="0px"/>
    <stroke android:width="0dp" android:color="#FFFFFF"/>
</shape>


Emoji=ThumbsUp

即兴,我假设您将使用
RelativeLayout
FrameLayout
作为地图和
工具栏的容器。将
工具栏
作为容器中的后续子项,将使其具有更高的高程,并使其浮动在地图表面上。是。这样可以使工具栏浮动在地图表面上。但是如何给工具栏“分离”的感觉(如图所示)。谢谢。我不太明白你的意思。如果你指的是drop shadow,那应该是Android 5.0上的“开箱即用”。在安卓4.4及以下版本上,您可能会看到
cardwiew
如何实现它的假阴影,并试图复制它。很抱歉上面的评论不够清晰。我再试一次。常规工具栏占据全部宽度。但在上图中,工具栏不是全宽的。因此给它一种“独立”的感觉。好吧,我还没有玩过
工具栏
,但因为它是一个普通的小部件,我假设它会响应类似于
android:layout\u width
。请将代码编辑成代码块,并提供更多细节来支持您的答案
<LinearLayout
    android:background="@color/colorWhite"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="16dp">


<androidx.appcompat.widget.Toolbar
    android:background="@drawable/floating_toolbar"
    android:id="@+id/app_toolbar"
    android:elevation="2dp"
    android:layout_width="match_parent"
    android:layout_height="50dp">

<EditText
    android:gravity="start"
    android:id="@+id/search_bar"
    android:background="@drawable/edit_text_no_border"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="Search here.." />

</androidx.appcompat.widget.Toolbar>       
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
    <shape
        android:shape="rectangle">
        <solid android:color="#6FA5A5A5" />
        <corners android:radius="6dp"/>
    </shape>
</item>
<item android:top="1dp" android:right="1dp" android:left="1dp" android:bottom="1dp">
    <shape
        android:shape="rectangle">
        <solid android:color="@color/colorWhite"/>
        <corners android:radius="5dp"/>
    </shape>
</item>
</layer-list>
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <solid android:color="#ffffff"/>
    <corners  android:radius="0px"/>
    <stroke android:width="0dp" android:color="#FFFFFF"/>
</shape>