【问题标题】:Creating and positioning a Floating Action Button创建和定位浮动操作按钮
【发布时间】:2015-06-25 15:58:03
【问题描述】:

我正在尝试将 FAB 添加到卡片视图的底部,例如在此应用中:

该按钮确实显示了,但它不与卡片视图重叠。我正在使用Android Material Design Library。这是我的文件:

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>

activity_main.xml:

    tools:context=".MainActivity">

    <include
        android:id="@+id/toolbar"
        layout="@layout/toolbar" />
    <LinearLayout
        xmlns:card_view="http://schemas.android.com/apk/res-auto"
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:id="@+id/main_content"
        android:layout_below="@id/toolbar">

        <android.support.v7.widget.CardView
            xmlns:card_view="http://schemas.android.com/apk/res-auto"
            android:id="@+id/stop_card"
            android:layout_gravity="center"
            android:layout_width="match_parent"
            android:layout_height="100dp"
            android:layout_marginTop="5dp"
            card_view:cardCornerRadius="4dp">

            <LinearLayout
                android:orientation="vertical"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:paddingLeft="10dp"
                android:paddingTop="5dp"
                android:paddingRight="10dp"
                android:paddingBottom="5dp">

                <TextView
                    android:id="@+id/label_stop"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:text="Fermata"
                    android:textSize="25sp"
                    android:textStyle="bold"
                    android:singleLine="true" />

                <EditText
                    android:id="@+id/edit_stop"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:inputType="number"
                    android:ems="10"
                    android:layout_gravity="end" />
            </LinearLayout>
        </android.support.v7.widget.CardView>

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

            <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>
    </LinearLayout>
</RelativeLayout>

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 android-layout android-button material-design


【解决方案1】:

您可以尝试向按钮或其父级添加负上边距,以便向上移动。由于您似乎想将其与卡片正好重叠一半,您可以将android:layout_marginTop="-36dp" 添加到按钮的父级RelativeLayout,其中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 是否有一些elevation?在这种情况下,您可能已经为您的 FAB 设置了相同(或更大)的标高。
  • 这很奇怪...我向 CardView 添加了 4dp 的高度,向按钮添加了 8dp 的高度,并且按钮从视图中消失了。当我移除 CardView 高程时也发生了同样的情况!
  • 您也可以尝试将 CardView 和 FAB 包装在 RelativeLayout 中并根据需要对齐它们,如下所示:gist.github.com/Floern/12e7d40ed3b6539e72e1
  • 感谢您为此付出的所有努力。但是,我仍然有问题。当我使用您的代码时,FAB 一直到屏幕底部。我无法将其向上移动。
猜你喜欢
  • 1970-01-01
  • 2015-05-15
  • 1970-01-01
  • 2016-11-27
  • 1970-01-01
  • 2015-09-23
  • 2014-12-26
  • 1970-01-01
  • 2023-04-04
相关资源
最近更新 更多