我猜你已经看到了新的Android设计指南,新的“浮动动作按钮”,又名“FAB”
比如这个粉色按钮:
我的问题听起来很愚蠢,我已经尝试了很多东西,但是把这个按钮放在两个布局的交集的最佳方法是什么?
在上面的例子中,这个按钮被完美地放置在我们想象中的ImageView和relativeLayout之间。
我已经尝试了很多调整,但我相信有一个正确的方法。
我猜你已经看到了新的Android设计指南,新的“浮动动作按钮”,又名“FAB”
比如这个粉色按钮:
我的问题听起来很愚蠢,我已经尝试了很多东西,但是把这个按钮放在两个布局的交集的最佳方法是什么?
在上面的例子中,这个按钮被完美地放置在我们想象中的ImageView和relativeLayout之间。
我已经尝试了很多调整,但我相信有一个正确的方法。
当前回答
最佳实践:
将编译'com.android.support:design:25.0.1'添加到gradle文件中 使用CoordinatorLayout作为根视图。 将layout_anchor添加到FAB并将其设置为俯视图 将layout_anchorGravity添加到FAB,并设置为:bottom|right|end
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:id="@+id/viewA"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="0.6"
android:background="@android:color/holo_purple"
android:orientation="horizontal"/>
<LinearLayout
android:id="@+id/viewB"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="0.4"
android:background="@android:color/holo_orange_light"
android:orientation="horizontal"/>
</LinearLayout>
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:clickable="true"
android:src="@drawable/ic_done"
app:layout_anchor="@id/viewA"
app:layout_anchorGravity="bottom|right|end"/>
</android.support.design.widget.CoordinatorLayout>
其他回答
您可以在Android Studio中导入谷歌的样例项目,点击File > import sample…
这个示例包含一个FloatingActionButton视图,它继承自FrameLayout。
编辑 使用新的支持设计库,你可以像下面这样实现它: https://github.com/chrisbanes/cheesesquare
将此添加到您的gradle文件中
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:23.0.0'
compile 'com.android.support:design:23.0.1'
}
到activity_main.xml
<android.support.design.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:id="@+id/viewOne"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="0.6"
android:background="@android:color/holo_blue_light"
android:orientation="horizontal"/>
<LinearLayout
android:id="@+id/viewTwo"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="0.4"
android:background="@android:color/holo_orange_light"
android:orientation="horizontal"/>
</LinearLayout>
<android.support.design.widget.FloatingActionButton
android:id="@+id/floatingButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:clickable="true"
android:src="@drawable/ic_done"
app:layout_anchor="@id/viewOne"
app:layout_anchorGravity="bottom|right|end"
app:backgroundTint="#FF0000"
app:rippleColor="#FFF" />
</android.support.design.widget.CoordinatorLayout>
你可以在http://www.ahotbrew.com/android-floating-action-button/上下载android studio项目的完整示例
现在它是官方设计支持库的一部分。
在你的gradle里:
compile 'com.android.support:design:22.2.0'
http://developer.android.com/reference/android/support/design/widget/FloatingActionButton.html
似乎这个例子中最简洁的方法是:
使用相对布局 将相邻的2个视图一个放在另一个下面 将FAB对齐到父的右/端,并添加右/端边距 将FAB对齐到头部视图的底部,并添加一个负边距,即FAB大小的一半,包括阴影
例子改编自萨满国实现,使用任何你想要的FAB。假设FAB是64dp高,包括阴影:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<View
android:id="@+id/header"
android:layout_width="match_parent"
android:layout_height="120dp"
/>
<View
android:id="@+id/body"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/header"
/>
<fully.qualified.name.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignBottom="@id/header"
android:layout_marginBottom="-32dp"
android:layout_marginRight="20dp"
/>
</RelativeLayout>
这是另外一个免费的Android浮动动作按钮库。 它有许多自定义,需要SDK版本9及更高版本
完整演示视频
dependencies {
compile 'com.scalified:fab:1.1.2'
}