首页 > 解决方案 > 工具栏出现在 android 应用程序的底部而不是顶部

问题描述

我正在为我的 android 应用程序使用工具栏并禁用操作栏。对于主要活动,工具栏正确出现在顶部,但对于应用程序设置(这是一个片段),它出现在底部,对此有任何帮助

<fragment xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:name="com.example.app.Settings$NewsPreferenceFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_below="@id/toolbar"
    tools:context="com.example.app.Settings">
<include xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/toolbar"
    layout="@layout/toolbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

标签: androidandroid-layoutandroid-fragments

解决方案


请检查您是否使用具有重力=“底部”或相对布局的线性布局

确保代码驻留在这样的地方

理想情况下,布局应该看起来像

<LinearLayout 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" 
    orientation="vertical">

<include xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/toolbar"
    layout="@layout/toolbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

<fragment     
    android:name="com.example.app.Settings$NewsPreferenceFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.app.Settings">
</LinearLayout>

推荐阅读