FloatingActionButton with text instead of image

后端 未结 9 1100
说谎
说谎 2020-12-01 00:09

I\'m trying to figure out how can be modified FloatingActionButton from android support library. Can it be used with the text instead of image?

Something like this o

相关标签:
9条回答
  • 2020-12-01 00:31

    With API 28 you can simply add text to Fabs using:

    Visit: https://material.io/develop/android/components/extended-floating-action-button/

     <com.google.android.material.floatingactionbutton.ExtendedFloatingActionButton
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_margin="8dp"
          android:contentDescription="@string/extended_fab_content_desc"
          android:text="@string/extended_fab_label"
          app:icon="@drawable/ic_plus_24px"
          app:layout_anchor="@id/app_bar"
          app:layout_anchorGravity="bottom|right|end"/>
    
    0 讨论(0)
  • 2020-12-01 00:34

    I was needing text in a FAB but instead I just went with a TextView with a circular drawable background:

      <TextView
            android:layout_margin="10dp"
            android:layout_gravity="right"
            android:gravity="center"
            android:background="@drawable/circle_background"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="#FFF"
            android:textStyle="bold"
            android:fontFamily="sans-serif"
            android:text="AuthId"
            android:textSize="15dp"
            android:elevation="10dp"/>
    

    Here is the drawable(circle_backgroung.xml):

    <?xml version="1.0" encoding="utf-8"?>
    <shape
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:shape="oval">
    
    <solid
        android:color="#666666"/>
    
    <size
        android:width="60dp"
        android:height="60dp"/>
    </shape>
    

    0 讨论(0)
  • 2020-12-01 00:37

    Thanks to all.

    Here is easy workaround which I found for this question. Works correctly for Android 4+, for Android 5+ is added specific parameter android:elevation to draw TextView over FloatingActionButton.

    <FrameLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|right">
    
        <android.support.design.widget.FloatingActionButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@android:color/transparent" />
    
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:text="@android:string/ok"
            android:elevation="16dp"
            android:textColor="@android:color/white"
            android:textAppearance="?android:attr/textAppearanceMedium" />
    </FrameLayout>
    
    0 讨论(0)
提交回复
热议问题