我猜你已经看到了新的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>
其他回答
现在它是官方设计支持库的一部分。
在你的gradle里:
compile 'com.android.support:design:22.2.0'
http://developer.android.com/reference/android/support/design/widget/FloatingActionButton.html
最佳实践:
将编译'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浮动动作按钮库。 它有许多自定义,需要SDK版本9及更高版本
完整演示视频
dependencies {
compile 'com.scalified:fab:1.1.2'
}
在AppCompat 22中,FAB支持旧设备。
在build.gradle(app)中添加新的支持库:
compile 'com.android.support:design:22.2.0'
然后你可以在你的xml中使用它:
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:src="@android:drawable/ic_menu_more"
app:elevation="6dp"
app:pressedTranslationZ="12dp" />
要使用elevation和pressedTranslationZ属性,需要namespace app,所以将这个namespace添加到你的布局中: xmlns:应用= " http://schemas.android.com/apk/res-auto "
您可以在Android Studio中导入谷歌的样例项目,点击File > import sample…
这个示例包含一个FloatingActionButton视图,它继承自FrameLayout。
编辑 使用新的支持设计库,你可以像下面这样实现它: https://github.com/chrisbanes/cheesesquare