提问者:小点点

将图标放置在MaterialButton中的文本顶部


但我在MaterialButton中找不到任何属性来将app:图标的重力设置为顶部。 我只能设置它的开始或结束。 有没有任何方法可以在不使用ImageView和TextView的垂直线性布局的情况下设置create a similable按钮?


共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"/>

匿名用户

下一个版本将提供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-Snapshot版本中可用。