Android 创建和定位浮动操作按钮

Android 创建和定位浮动操作按钮,android,android-layout,android-button,material-design,Android,Android Layout,Android Button,Material Design,我正在尝试将晶圆厂添加到卡片视图的底部,如此应用程序中所示: 按钮确实会显示,但不会与卡片视图重叠。我正在使用。这是我的档案: colors.xml: <?xml version="1.0" encoding="utf-8"?> <resources> <color name="colorPrimary">#3F51B5</color> <color name="colorPrimaryDark">#303F9F<

我正在尝试将晶圆厂添加到卡片视图的底部,如此应用程序中所示:

按钮确实会显示,但不会与卡片视图重叠。我正在使用。这是我的档案:

colors.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="colorPrimary">#3F51B5</color>
    <color name="colorPrimaryDark">#303F9F</color>
    <color name="colorAccent">#FFEB3B</color>
    <color name="colorAccentDark">#FBC02D</color>
</resources>
build.gradle

apply plugin: 'com.android.application'

android {
    compileSdkVersion 22
    buildToolsVersion "22.0.1"

    defaultConfig {
        applicationId "com.example.john.gttime"
        minSdkVersion 21
        targetSdkVersion 22
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:21.0.+'
    compile 'com.android.support:cardview-v7:21.0.+'
    compile 'com.android.support:recyclerview-v7:21.0.+'
    compile 'com.github.navasmdc:MaterialDesign:1.+@aar'
}
这几乎是对的,但它应该是卡视图的一半。这就是我在Android Studio预览中看到的内容:

您可以尝试向按钮或其父按钮添加负上边距,以便将其向上移动。由于您似乎希望将其与卡正好重叠一半,您可以将一个
android:layout_marginTop=“-36dp”
添加到按钮的父相对位置,其中
36dp
是其高度的一半:

    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="72dp"
        android:layout_marginTop="-36dp">

        <com.gc.materialdesign.views.ButtonFloat
            android:id="@+id/buttonFloat"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="#1E88E5"
            materialdesign:animate="true"
            materialdesign:iconDrawable="@android:drawable/ic_menu_search"
            android:layout_gravity="end"
            android:layout_alignParentTop="true"
            android:layout_alignParentEnd="true" />

    </RelativeLayout>


谢谢!事实上它可以工作,但不幸的是按钮的上半部分在卡的下面。我该如何解决这个问题?非常感谢。CardView是否有一些
标高
?在这种情况下,你可能已经为你的工厂设置了相同(或更大)的标高。这很奇怪。。。我给CardView添加了4dp的高程,给按钮添加了8dp的高程,按钮从视图中消失了。当我移除CardView立面时也发生了同样的情况!您也可以尝试将CardView和晶圆厂包装在一个相对的图中,并根据需要对齐它们,如下所示:感谢您为此付出的所有努力。但是,我仍然有问题。当我使用你的代码时,晶圆厂一直到屏幕底部。我无法向上移动。这个问题在@mihirsah有答案。我没有看到,谢谢!
    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="72dp"
        android:layout_marginTop="-36dp">

        <com.gc.materialdesign.views.ButtonFloat
            android:id="@+id/buttonFloat"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="#1E88E5"
            materialdesign:animate="true"
            materialdesign:iconDrawable="@android:drawable/ic_menu_search"
            android:layout_gravity="end"
            android:layout_alignParentTop="true"
            android:layout_alignParentEnd="true" />

    </RelativeLayout>