How to add a TextView above a FloatingActionButton

2019-03-11 11:36发布

I want to add a TextView above a FloatingActionButton, I use the FrameLayout as the parent layout of the TextView and FloatingActionButton, here is my sample code:

<FrameLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

    <android.support.design.widget.FloatingActionButton
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:backgroundTint="#00fff0"
        app:borderWidth="0dp"
        android:elevation="0dp"
        app:fabSize="normal" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="#000000"
        android:text="above"/>

</FrameLayout>

but it's useless, the TextView is below the FloatingActionButton, like this enter image description here

and I want it show like this: enter image description here

I am poor for this, can anyone help me?

5条回答
手持菜刀,她持情操
2楼-- · 2019-03-11 12:12

it a z-order problem. In your FrameLayout move the <TextView above the <FloatingActionButton

<FrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">

  <TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textColor="#000000"
    android:text="above"/>

  <android.support.design.widget.FloatingActionButton
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/fab"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:backgroundTint="#00fff0"
    app:borderWidth="0dp"
    android:elevation="0dp"
    app:fabSize="normal" />
</FrameLayout>

should do it

查看更多
可以哭但决不认输i
3楼-- · 2019-03-11 12:12

Here is your solution :

<FrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">

<RelativeLayout 
android:layout_width="wrap_content"
android:layout_height="wrap_content" >


<android.support.design.widget.FloatingActionButton
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/fab"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:backgroundTint="#00fff0"
    app:borderWidth="0dp"
    android:elevation="0dp"
    app:fabSize="normal" />

<TextView
    android:layout_toRightOf="@+id/fab"
    android:layout_marginLeft="-10dp"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textColor="#000000"
    android:text="above"/>
</RelativeLayout>

</FrameLayout>
查看更多
霸刀☆藐视天下
4楼-- · 2019-03-11 12:14

To support low end device use both android:elevation and app:elevation

set both values to 0 for Fab

 <FrameLayout
       android:layout_width="wrap_content"
       android:layout_height="wrap_content">

<android.support.design.widget.FloatingActionButton
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/fab"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:backgroundTint="#00fff0"
    app:borderWidth="0dp"
    android:elevation="0dp"
    app:elevation="0dp"
    app:fabSize="normal" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textColor="#000000"
    android:text="above"/>

    </FrameLayout>
查看更多
ら.Afraid
5楼-- · 2019-03-11 12:24

Set elevations for both views accordingly. Also, it makes more sense for you to move your TextView XML code above your FAB XML code.

查看更多
Anthone
6楼-- · 2019-03-11 12:26

you just need to elevation attribute to the textview

 <FrameLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

    <android.support.design.widget.FloatingActionButton
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:backgroundTint="#00fff0"
        app:borderWidth="0dp"
        android:elevation="0dp"
        app:fabSize="normal" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="#000000"
        android:text="above"
        android:elevation="7dp"/>

</FrameLayout>
查看更多
登录 后发表回答