简体   繁体   中英

Placing icon on top of text in MaterialButton

I wanted to put the icon on top of the materialButton like shown here: 在此处输入图像描述

But I couldn't find any properties in MaterialButton to set the gravity of the app:icon to top. I could only able to set it to start or end. Is there any way to set the create a similar button without using Vertical LinearLayout with ImageView and TextView?

You can use android:drawableTop attribute for this

Note: This is not only restricted for the MaterialButton you can use it on TextViews and other views also

Example Code:

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

You can use the iconGravity="textTop" .

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

在此处输入图像描述

Note: It requires at least the version 1.3.0-alpha02 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM