【问题标题】:Placing icon on top of text in MaterialButton将图标放在 MaterialButton 中的文本顶部
【发布时间】:2020-10-25 01:10:14
【问题描述】:

我想将图标放在 materialButton 的顶部,如下所示:

但我在 MaterialButton 中找不到任何属性来将 app:icon 的重力设置为顶部。 我只能将其设置为开始或结束。 有没有办法在不使用带有 ImageView 和 TextView 的 Vertical LinearLayout 的情况下设置创建类似按钮?

【问题讨论】:

  • 在此上下文中您指定为 MaterialButton 的内容?实际上,哪个是图标?
  • 通话按钮、网站按钮、保存按钮

标签: android android-layout material-design android-button material-components-android


【解决方案1】:

您可以使用iconGravity="textTop"

    <com.google.android.material.button.MaterialButton
        style="@style/Widget.MaterialComponents.Button.TextButton"
        app:icon="@drawable/ic_add_24px"
        app:iconGravity="textTop" />

注意:至少需要1.3.0-alpha02版本。

【讨论】:

【解决方案2】:

您可以为此使用android:drawableTop 属性

注意:这不仅限于您可以使用的 MaterialButton 它也在 TextViews 和其他视图上

示例代码:

<com.google.android.material.button.MaterialButton
    android:layout_width="match_parent"
    android:text="@string/app_name"  <!-- Your Text -->
    android:drawableTop="@drawable/ic_launcher_background" <!-- Your Icon -->
    android:layout_height="wrap_content"/>

【讨论】:

  • 它可以工作,但使用compoundDrawables(例如android:drawableTop!= 使用iconGravity
猜你喜欢
  • 2011-02-24
  • 2013-09-16
  • 2013-05-12
  • 1970-01-01
  • 1970-01-01
  • 2018-07-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多