【问题标题】:Show only icon in floating action button在浮动操作按钮中仅显示图标
【发布时间】:2020-02-22 13:35:02
【问题描述】:

我只想在 FloatingActionButton 中显示我的可绘制图标(至少 14 API 级别和更高级别)。我尝试使用 src、背景、更改背景色调、边框宽度等各种东西,但没有任何帮助。即使改变我的drawable的大小也不会改变它的显示大小。它很小,被按钮背景包围。我希望它更大而且没有背景。

附言。不想用 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" />

【问题讨论】:

  • 你试过把 'android:background="#0000"' 或 'app:backgroundTint="@null"' 放进去吗?
  • 没有帮助,解决方案如下
  • 也许这样做更容易,但我使用了 stringbuilder 为其添加了一个图标。

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


【解决方案1】:

试试这个:

<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;没找到其他办法。

祝你有美好的一天。

【讨论】:

    【解决方案2】:

    只需使用类似的东西:

      <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>
    

    正常和按下状态结果:

    【讨论】:

    • 是的,但是当我添加高程 0dp 时,我无法单击浮动按钮...?
    • 它可以工作(我使用的是 1.1.0 版本)。海拔不会改变onClickListener。如果您没有在 app:tint 中定义选择器(如答案所示),则用户在按钮上没有证据。
    猜你喜欢
    • 1970-01-01
    • 2020-07-17
    • 2019-04-05
    • 2016-11-09
    • 2019-09-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-18
    相关资源
    最近更新 更多