【问题标题】:Adding floating action button with drawable layout is not visible in Android使用可绘制布局添加浮动操作按钮在 Android 中不可见
【发布时间】:2016-03-30 03:28:32
【问题描述】:

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

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

这是主要的布局 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


    【解决方案1】:

    尝试将您的LinearLayout 更改为RelativeLayout

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

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

    app_bar_main.xml

    <?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,例如您的应用。 希望对您有所帮助

    【讨论】:

    • 那么我的抽屉布局如何?我也需要导航抽屉。请
    • @WaiYanHein 只需将您的LinearLayout 更改为RelativeLayout
    • @WaiYanHein 请再次查看我的RelativeLayout。你忘记了我的RelativeLayout 代码中的某些部分
    • 我有一些评论//在我的RelativeLayout中添加这个。你把它放到你的布局中了吗?
    • 非常感谢。它工作得很好。我会投票赞成答案。
    猜你喜欢
    • 2020-01-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-07
    • 2020-02-26
    • 2018-01-01
    • 2015-09-23
    • 1970-01-01
    相关资源
    最近更新 更多