android - 工具栏下的 fragment

标签 android xml android-fragments toolbar

我以前从未在我的项目中使用过 menu + fab。所以我制作了 3 个选项卡 fragment ,所有 3 个都显示了 ListView ,但我的 fragment 在工具栏下面

这是代码 - fragment.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">

<TextView
    android:id="@+id/heading"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingLeft="14dp"
    android:paddingTop="16dp"
    android:text="VEHICLES REQUESTED"
    android:textColor="#88bf40"
    android:textStyle="bold"/>

<ListView
    android:id="@+id/vehicle_LV"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"/>

</android.support.constraint.ConstraintLayout>

我正在使用它或者第一次使用 fab 和菜单导航,如果我在这里有什么问题请告诉我。

MainActivity xml 代码 - activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">

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

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/AppTheme.PopupOverlay" />

        <android.support.design.widget.TabLayout
            android:id="@+id/tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <android.support.design.widget.TabItem
                android:id="@+id/tabItem"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="1" />

            <android.support.design.widget.TabItem
                android:id="@+id/tabItem2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="2" />

            <android.support.design.widget.TabItem
                android:id="@+id/tabItem3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="3" />

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

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

    <android.support.v4.view.ViewPager
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/viewPager"/>

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        app:backgroundTint="#303440"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|end"
        android:layout_margin="@dimen/fab_margin"
        app:srcCompat="@android:drawable/ic_input_add" />

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

<android.support.design.widget.NavigationView
    android:id="@+id/nav_view"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:fitsSystemWindows="true"
    app:headerLayout="@layout/nav_header_main"
    app:menu="@menu/activity_main_drawer" />

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

提供 margin 是一种方法,但我怀疑这是解决此问题的正确方法。有人知道该怎么做吗?

屏幕截图 - here

最佳答案

这适用于正在寻找同一问题答案的所有其他人。

所以问题出在我的主题上,它默认设置为无操作栏。 所以我将其更改为带有操作栏的主题。

这是我添加到我的 xml 中的代码。

android:theme="@style/AppTheme"

这是我在 styles.xml 文件中添加的代码

<style name="AppTheme" parent="Base.Theme.AppCompat.Light.DarkActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
</style>

关于android - 工具栏下的 fragment ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52475285/

相关文章:

Android 多面板工具栏

Android 蓝牙 LE 扫描设备缺少名称

android - 如何更改 Spinner 运行时的文本大小

java - 在 Activity 映射 xml 中膨胀类 fragment 时出错

即使设置了lifecycleOwner,Android Fragment ViewModel数据绑定(bind)也不会更新UI

android - 查看寻呼机 2 : Depth Page Transformation make fragment click events disable on pager swipe up

xml - 如何过早退出模板?

xml - 关闭 Saxon 中的 xml header 输出

xml - 如何使用属性值作为 XML 多态类型选择的鉴别器?

android - 在 Kotlin 中使用 Android 数据绑定(bind)初始化 RecyclerView 会引发错误。我究竟做错了什么?