以编程方式更改浮动按钮图像的大小 [英] Changing floating button image size programmatically

查看:94
本文介绍了以编程方式更改浮动按钮图像的大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我里面有浮动按钮和图像.我需要更改图像的大小来填充里面的所有空间,例如:|

I have floating button and image inside. And I need image to change size filling all space inside like here:|

我尝试了不同的秤类型,但是没有用.我现在得到了这个结果.

I tried different scale types but it did't work. I got this result for now.

最后,我找到了解决方案-maxImageSize.它起作用了,但是现在我需要以编程方式设置Fbutton的大小,而我找不到在代码中更改maxImageSize的方法.

Finally I found the solution - maxImageSize. It worked, but now I need to set Fbutton size programmatically and I can't find the way to change maxImageSize in code.

推荐答案

在XML中设置maxImageSize时,您正在更改FAB的常规行为和图标大小.

When you set the maxImageSize in the XML, you are changing the normal behavior and icon size of the FAB.

<com.google.android.material.floatingactionbutton.FloatingActionButton
            android:adjustViewBounds="true"
            android:id="@+id/fab_main"
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:src="@drawable/ic_home"
            app:backgroundTint="@color/colorBottomAppBarFab"
            app:fabSize="normal"
            app:layout_anchor="@+id/bottom_app_bar"
            app:maxImageSize="56dp"/>

此代码的结果是:图像大小为56dp的FAB

以后可以再次但以编程方式更改它,可以使用ScaleType更改图标尺寸. 这是Kotlin中的一个示例:

Later to change it again but programatically, you can use ScaleType to change the icon dimensions. Here an example in Kotlin:

fab_main.scaleType = ImageView.ScaleType.CENTER_INSIDE

然后,这是新结果: 具有较小图标尺寸的FAB

And then, here is the new result: FAB with smaller icon size

如果要更改FAB尺寸,可能使用以下方法之一可以为您提供帮助: FAB方法

If you want to change FAB dimensions, may be using one of the following methods can help you: FAB methods

我希望此解决方案可以为您提供帮助.

I hope this solution can help you.

这篇关于以编程方式更改浮动按钮图像的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆