如何在android中的FAB下添加文本

时间:2016-06-13 12:32:17

标签: android android-layout

我正在使用Android的应用程序,我需要在FAB(浮动操作按钮)下放置一个文本,如下所示:

enter image description here

但每次我尝试使用LinearLayout或RelativeLayout时,FAB只会设置为随机位置,而不是我需要它。

我无法发布此布局的所有代码,因为它有一些我无法分享的详细信息(我在合同下,这不是公共项目)。因此,如果您需要任何具体的信息,请告诉我,我会找到一种方法来分享它,而不会泄露任何内容。

编辑:我需要它作为FAB,因为在我点击布局上的某些内容后,它应该如下所示: FAB after click

如果你点击图片,它会转到另一个页面

1 个答案:

答案 0 :(得分:1)

您是否使用CoordinatorLayout来设置FAB的位置?

以下是一个如何做到这一点的例子。

<android.support.design.widget.CoordinatorLayout
android:id="@+id/main_content"
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">

    <!-- YOUR CONTENT -->

    <LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    app:layout_anchor="@id/main_content"
    app:layout_anchorGravity="bottom|right|end">

        <android.support.design.widget.FloatingActionButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|right"
        android:layout_margin="16dp"
        android:src="@mipmap/ic_launcher"/>

        <-- PUT HERE YOUR TEXTVIEW -->

    </LinearLayout>

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

编辑:改变anchorGravity

CoordinatorLayout.LayoutParams p = (CoordinatorLayout.LayoutParams) fab.getLayoutParams();
p.setAnchorId(xxxx);
fab.setLayoutParams(p);