浮动操作按钮中仅显示图标

时间:2019-10-26 13:52:54

标签: android floating-action-button material-components material-components-android

我只想在FloatingActionButton中显示我的可绘制图标(至少14 API或更高级别)。我尝试了src,背景,更改的背景色,borderWidth,各种各样的东西,但没有任何帮助。即使更改我的可绘制对象的大小也不会更改其显示大小。它很小,周围有按钮背景。我希望它更大并且没有背景。

PS。不想使用ImageView这样做,并且会失去FloatingActionButton提供的所有功能。

 <com.google.android.material.floatingactionbutton.FloatingActionButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="20dp"
        android:layout_marginRight="20dp"
        android:layout_marginBottom="20dp"
        android:src="@drawable/ic_add_circle"
        app:borderWidth="0dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintRight_toRightOf="parent" />

2 个答案:

答案 0 :(得分:2)

尝试一下:

<com.google.android.material.floatingactionbutton.FloatingActionButton
    android:id="@+id/faBtn"

    android:layout_width="wrap_content"
    android:layout_height="wrap_content"

    android:layout_marginEnd="20dp"
    android:layout_marginRight="20dp"
    android:layout_marginBottom="20dp"

    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintRight_toRightOf="parent"


    android:src="@drawable/ic_add_circle"

    app:fabCustomSize="@dimen/fabSize"
    app:fabSize="normal"
    app:maxImageSize="@dimen/fabImageSize"

    android:background="@null"
    android:backgroundTint="@null"

    app:backgroundTint="@null"
    app:elevation="0dp"
    android:elevation="0dp"
/>

要使晶圆厂不投射阴影,您需要将仰角设置为0;没有找到其他方法。

今天愉快。

答案 1 :(得分:0)

只需使用以下内容:

  <com.google.android.material.floatingactionbutton.FloatingActionButton
      android:id="@+id/fab_test"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"

      app:srcCompat="@drawable/baseline_search_24"
      app:tint="@color/selector_button"
      app:backgroundTint="@android:color/transparent"
      app:elevation="0dp"
      app:maxImageSize="@dimen/design_fab_size_mini"
      app:pressedTranslationZ="0dp"
      ../>
  • app:srcCompat添加图标(而不是android:src
  • app:backgroundTint为背景定义透明颜色
  • app:elevation="0dp"移除洗颜
  • app:pressedTranslationZ="0dp"按下时删除FAB的TranslationZ
  • app:maxImageSize定义图标的最大尺寸。您可以使用自定义值或标准值,例如design_fab_size_mini = 40dpdesign_fab_size_normal = 56dp
  • app:tint:定义图标的颜色。您可以使用选择器来处理按下状态。

类似的东西:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
  <item android:color="@color/mycolor" android:state_enabled="true" android:state_pressed="true"/>
  <item android:color="@color/myPrimaryColor" android:state_enabled="true"/>
  <item android:alpha="0.12" android:color="?attr/colorOnSurface"/>
</selector>

正常和按下状态结果:

enter image description here enter image description here