FloatingActionButton 不显示在应用程序上

FloatingActionButton does not show up on app

我想在 Android 中创建一个类似于 Gmail 收件箱的简单布局。项目列表和用于添加新项目的 FloatingActionButton。在 Android Studio 的设计选项卡中,显示了该按钮。但是,当我在模拟器中启动该应用程序时,该按钮没有显示。我正在使用 recyclerview 来显示项目。项目正确显示。

片段代码

    <FrameLayout
    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.support.v7.widget.RecyclerView
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/question_recycler_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    </android.support.v7.widget.RecyclerView>

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/floatingActionButton2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/icon_add"
        android:layout_gravity="end|bottom"
        android:layout_margin="16dp"/>

</FrameLayout>

Activity布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    android:layout_height="match_parent"
    android:layout_width="wrap_content"
    xmlns:tools="http://schemas.android.com/tools">


    <android.support.design.widget.AppBarLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

        <android.support.design.widget.TabLayout
            android:id="@+id/dashboard_tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:tabMode="fixed"
            app:tabGravity="fill">
        </android.support.design.widget.TabLayout>

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

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

依赖关系

apply plugin: 'com.android.application'

android {
    compileSdkVersion 26
    defaultConfig {
    applicationId "com.tl.dialog"
    minSdkVersion 19
    targetSdkVersion 26
    versionCode 1
    versionName "1.0"
    testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
  }
}



 dependencies {
        implementation fileTree(include: ['*.jar'], dir: 'libs')
        implementation 'com.android.support.constraint:constraint-layout:1.0.2'
        testImplementation 'junit:junit:4.12'
        androidTestImplementation 'com.android.support.test:runner:1.0.1'
        androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
        implementation 'com.android.support:recyclerview-v7:26.1.0'
        implementation 'com.android.support:cardview-v7:26.1.0'
        implementation 'com.android.support:appcompat-v7:26.1.0'
        implementation 'com.android.support:support-v4:26.1.0'
        implementation 'com.android.support:design:26.1.0'

        compileOnly 'org.projectlombok:lombok:1.16.20'
        annotationProcessor 'org.projectlombok:lombok:1.16.20'
    }

你 return 来自 onCreateView()RecyclerView 而不是根 View。 (我想这是因为在某些早期版本中你只有 Fragment 里面的 RecyclerView

所以你只需要更改 onCreateView() 的最后一行,而不是 return recycler; 应该是

return mView;