【问题标题】:How can I have a toolbar stay at top of screen instead of showing up on the whole screen?如何让工具栏留在屏幕顶部而不是显示在整个屏幕上?
【发布时间】:2015-03-07 08:10:52
【问题描述】:

这可能是我处理布局方式的问题。 Java 和 Android SDK 的新手。我正在使用安卓工作室。

此时我的目标是让应用程序运行起来,该应用程序的屏幕底部显示一个带有图标的导航栏,顶部显示一个带有“关闭”按钮和“保存”按钮的工具栏。工具栏和导航栏之间应该是一个空白的白色屏幕。

但我无法让工具栏停留在屏幕顶部。灰色背景占据了白色背景前面的整个屏幕。 Here's截图。

这就是我所拥有的:

<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>

【问题讨论】:

    标签: java android layout android-studio toolbar


    【解决方案1】:

    您有点忽略了Toolbar 的意义。在Toolbar preview 阅读有关它的信息。你在另一端使用RelativeLayout 作为Toolbar,这很浪费。

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

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

    这是假设你的根布局是RelativeLayout

    【讨论】:

      猜你喜欢
      • 2016-10-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-10-23
      • 1970-01-01
      • 2021-06-28
      • 1970-01-01
      相关资源
      最近更新 更多