当前位置: 首页 > 知识库问答 >
问题:

如何让工具栏停留在屏幕顶部,而不是显示在整个屏幕上?

周和歌
2023-03-14

这可能是我处理布局的方式有问题。对Java和Android软件开发工具包来说有点新鲜。我正在使用Android Studio。

我在这一点上的目标是有一个应用程序,屏幕底部显示一个带有图标的导航栏,顶部有一个带有“关闭”按钮和“保存”按钮的工具栏。工具栏和导航栏之间应该是一个空白的白色屏幕。

但是我很难让工具栏停留在屏幕的顶部。灰色背景占据了白色背景前面的整个屏幕。下面是截图。

这是我所知道的:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ContactActivity">

<RelativeLayout
    android:id="@+id/toolbar"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:background="@color/toolbar_background">

    <ToggleButton
        android:id="@+id/toggleButtonEdit"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentleft="true"
        android:layout_marginLeft="20dp"
        android:text="@string/toggle_button" />

    <Button
        android:id="@+id/buttonSave"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_marginRight="20dp"
        android:text="Save" />

    <RelativeLayout
        android:id="@+id/navbar"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:background="@color/navbar_background"
        android:gravity="center_horizontal">

        <ImageButton
            android:id="@+id/imageButtonList"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:layout_marginRight="20dp"
            android:contentDescription="Contact List Button"
            android:src="@drawable/contactlisticon" />
        <ImageButton
            android:id="@+id/imageButtonMap"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_toRight0f="@id/imageButtonList"
            android:contentDescription="Contact Map Button"
            android:src="@drawable/mapicon" />
        <ImageButton
            android:id="@+id/imageButtonSettings"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="20dp"
            android:contentDescription="Settings Button"
            android:layout_toRight0f="@id/imageButtonMap"
            android:src="drawable/settingsicon" />


    </RelativeLayout>

共有1个答案

戎洛华
2023-03-14

你有点忽略了< code >工具栏的意义。在工具栏预览中阅读它。你在另一端使用< code>RelativeLayout作为< code >工具栏,这是一种浪费。

要使用底部工具栏,您可以使用另一个独立工具栏(如链接中所述)或使用LinearLayout,如下所示:

<LinearLayout
    android:id="@+id/check_in_bottom_toolbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true">
   ...
</LinearLayout>

这是假设您的根布局是相对布局

 类似资料:
  • 问题内容: 我想创建一个div,它位于内容块的下方,但是一旦页面滚动到足以接触其顶部边界的位置,它就会固定在适当的位置并随页面滚动。 问题答案: 您可以简单地使用css,将元素定位为fixed: 编辑: 您应该将元素的位置设为绝对,一旦滚动偏移量到达该元素,则应将其更改为fixed,并将顶部位置设置为零。 您可以使用scrollTop函数检测文档的顶部滚动偏移量: 当滚动偏移量达到200时,该元素

  • 我似乎无法理解如何显示多个通知而不让一个覆盖另一个。在我的例子中,它一次只显示一个。图片1 我的目标是让它像图2下面的截图一样工作 分配给通知的代码块

  • 我有旧式菜单应用程序。在改变主题到全息和切换目标SDK版本到14后,软菜单按钮消失了(这是好的),但使用菜单按钮在操作栏显示文本菜单大部分离屏幕,你可以看到在附上的图像。你有什么办法解决这个问题吗? 我已经解决了。清单参数中有问题: 在改变了任何否认为真之后,这个问题就消失了。

  • 试图制作一个简单的应用程序,从服务器获取JSON数据,并在自定义列表中显示它们,非常简单的事情。 但当我运行应用程序时,它显示的是白色空白屏幕,但没有数据。它也没有显示任何错误,我假设如果有任何错误,它不会在我的手机中运行。但不显示获取的数据。 下面是类 我发现的其他问题与我的问题不匹配,否则不会添加这个问题。

  • 问题内容: 我正在使用PIL库进行一些图像编辑。关键是,我不想每次都将图像保存在HDD上以在资源管理器中查看它。是否有一个小模块可以使我设置窗口并显示图像? 问题答案: 从PIL教程中: 一旦有了 Image 类的实例,就可以使用该类定义的方法来处理和操纵图像。例如,让我们显示刚刚加载的图像: 更新: 如今,该方法已正式记录在PIL的Pillow分支中,并说明了如何在不同的OS上实现该方法。