Jetpack Compose 中的材质图标大小调整?

And*_*ndy 11 android android-jetpack-compose

Jetpack compose 提供了一个很好的 Icon() 对象来显示接受矢量资源的图标。通常,您可以通过修饰符设置大小:

Icon(Icons.Filled.PersonPin, Modifier.preferredSize(64.dp)) 
Run Code Online (Sandbox Code Playgroud)

我的问题是,当我使用提供的系统矢量资产(即 Icons.Filled. 或 Icons.Default. 等)时,我似乎无法影响显示图标的大小。当我使用自己的资产时,事情会按预期进行。使用系统资产,修改器只会增加封闭“框”的 UI 占用空间,而图标在其中保持很小:

来自 IDE 预览的示例

使用 'then' 应用修饰符会导致相同的行为:

    Icon(Icons.Filled.PersonPin,Modifier.then(Modifier.preferredSize(128.dp)))
Run Code Online (Sandbox Code Playgroud)

有没有关于本机图标的东西?我假设是矢量资产,它们也应该能够调整大小。

nca*_*sas 14

接受的答案在 1.0.0-alpha11 中不再有效。这是相关的错误报告。根据错误报告评论,从 alpha12 开始执行此操作的新方法是:

Icon(Icons.Filled.Person, modifier = Modifier.size(128.dp)) 
Run Code Online (Sandbox Code Playgroud)


Mut*_*ran 9

内部材质图标大小为 24.dp

// All Material icons (currently) are 24dp by 24dp, with a viewport size of 24 by 24.
@PublishedApi
internal const val MaterialIconDimension = 24f
Run Code Online (Sandbox Code Playgroud)

并且使用修饰符中的大小它不起作用,因此我们可以通过复制图标来更改图标并更改默认高度和宽度。

Icon(Icons.Filled.Person.copy(defaultHeight = 128.dp, defaultWidth = 128.dp))
Run Code Online (Sandbox Code Playgroud)

注意:这不是设置图标大小的官方建议,只是更改图标大小的一种黑客方法。

  • 这在 1.0.0-alpha11 中不再有效(请参阅[错误报告](https://issuetracker.google.com/issues/178796190))。根据错误报告评论,从 alpha12 开始执行此操作的新方法是“Icon(Icons.Filled.Person, modifier = Modifier.size(128.dp))” (3认同)

Gab*_*tti 9

与1.0.x只使用Modifier.size(xx.dp)

Icon(Icons.Filled.Person,
    "contentDescription",
    modifier = Modifier.size(128.dp))
Run Code Online (Sandbox Code Playgroud)


Tob*_*ias 6

对我有用的是使用Modifier.fillMaxSize(...)),例如

Icon(Icons.Filled.Person, contentDescription = "Person", modifier = Modifier.fillMaxSize(0.5F))
Run Code Online (Sandbox Code Playgroud)

0.5

Icon(Icons.Filled.Person, contentDescription = "Person", modifier = Modifier.fillMaxSize(0.75F))
Run Code Online (Sandbox Code Playgroud)

0.75

Icon(Icons.Filled.Person, contentDescription = "Person", modifier = Modifier.fillMaxSize(1.0F))
Run Code Online (Sandbox Code Playgroud)

1.0