添加具有可绘制布局的浮动操作按钮在Android中不可见

添加具有可绘制布局的浮动操作按钮在Android中不可见,android,Android,我正在开发一个Android应用程序。在我的应用程序中,我使用抽屉布局作为主布局。主要内容的抽屉布局中有一个框架布局。单击项目表单抽屉时,主内容将替换为片段。片段可以包含listview、scroll、gridview或其他内容 我想要的是固定在屏幕右下角的浮动操作按钮。这必须在主布局(抽屉布局)中,因为它将包含所有替换的片段。我在主布局中添加了一个浮动操作按钮。但当我运行我的应用程序时,我看不到它 这是XML的主要布局: <?xml version="1.0" encoding="utf

我正在开发一个Android应用程序。在我的应用程序中,我使用抽屉布局作为主布局。主要内容的抽屉布局中有一个框架布局。单击项目表单抽屉时,主内容将替换为片段。片段可以包含listview、scroll、gridview或其他内容

我想要的是固定在屏幕右下角的浮动操作按钮。这必须在主布局(抽屉布局)中,因为它将包含所有替换的片段。我在主布局中添加了一个浮动操作按钮。但当我运行我的应用程序时,我看不到它

这是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"
    android:id="@+id/main_drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <!-- This LinearLayout represents the contents of the screen  -->

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <!-- The ActionBar displayed at the top -->
        <include
            layout="@layout/action_bar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

        <!-- The main content view where fragments are loaded -->
        <FrameLayout
            android:background="@color/whitesmoke"
            android:id="@+id/main_content"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />

        <android.support.design.widget.FloatingActionButton
            android:layout_gravity="bottom|end"
            android:id="@+id/fb_office_btn"
            android:src="@android:drawable/ic_menu_edit"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    </LinearLayout>

    <!-- The navigation drawer that comes from the left -->
    <!-- Note that `android:layout_gravity` needs to be set to 'start' -->
    <android.support.design.widget.NavigationView
        app:itemIconTint="@drawable/drawer_item"
        app:itemTextColor="@drawable/drawer_item"
        android:id="@+id/left_nv_view"
        app:headerLayout="@layout/header_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start" />

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

正如您在上面看到的,没有浮动操作按钮。我怎样才能修好它

编辑 当我将LinearLayout更改为RelativeLayout时,主要内容的gridview如下所示。动作条消失了


尝试将
线性布局更改为
相对布局

<android.support.v4.widget.DrawerLayout
...
>
...
          <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            >

            <!-- The ActionBar displayed at the top -->
            <include
                android:id="@+id/actionBar" // Add this
                layout="@layout/action_bar" 
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />

            <!-- The main content view where fragments are loaded -->
            <FrameLayout
                android:background="@color/whitesmoke"
                android:id="@+id/main_content"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_below="@id/actionBar" // Add this
             />

            <android.support.design.widget.FloatingActionButton
                android:layout_alignParentRight="true" // Add this
                android:layout_alignParentBottom="true" // Add this
                android:id="@+id/fb_office_btn"
                android:src="@android:drawable/ic_menu_edit"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />
        </RelativeLayout>
...
</android.support.v4.widget.DrawerLayout>

...
...
您可以遵循Android Studio设计

活动\u main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
    android:id="@+id/drawer_layout"
    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"
    android:fitsSystemWindows="true"
    tools:openDrawer="start">

    <include
        layout="@layout/app_bar_main" // include app_bar_main
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

    <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>
<?xml version="1.0" encoding="utf-8"?>
<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"
    android:fitsSystemWindows="true"
    tools:context=".ui.activities.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.AppBarLayout>

    <include layout="@layout/content_main"/>

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/feedback_fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|end"
        android:layout_margin="@dimen/fab_margin"
        android:src="@drawable/ic_fab_feedback"/>

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

app\u bar\u main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
    android:id="@+id/drawer_layout"
    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"
    android:fitsSystemWindows="true"
    tools:openDrawer="start">

    <include
        layout="@layout/app_bar_main" // include app_bar_main
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

    <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>
<?xml version="1.0" encoding="utf-8"?>
<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"
    android:fitsSystemWindows="true"
    tools:context=".ui.activities.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.AppBarLayout>

    <include layout="@layout/content_main"/>

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/feedback_fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|end"
        android:layout_margin="@dimen/fab_margin"
        android:src="@drawable/ic_fab_feedback"/>

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

更改某些组件(如应用程序)的id。
希望这有帮助

那么我的抽屉布局如何?我还需要一个导航抽屉。pls@WaiYanHein只需将您的
LinearLayout
更改为
RelativeLayout
@WaiYanHein请再次查看我的
RelativeLayout
。你忘了我的
RelativeLayout
code中的某个部分我有一些评论//请将此添加到我的RelativeLayout中。你把它放进你的版面了吗?非常感谢。它工作得很好。我将投票决定答案。