如何使行内的图像大小相同?

huangapple go评论111阅读模式
英文:

How do I get Images inside of rows to be the same size?

问题

我想在Jetpack Compose中在文本上方显示两个图像。为此,我创建了一个包含OnePlayer Composable的行。

这是OnePlayer Composable的代码

我希望两个图像都具有相同的大小,并且下方有文本。当我只在第二列中使用文本时,两个文本的大小都与我想要的一样。但是,一旦我添加了图像,左边的图像就比右边的大(请参见附图)。我该如何修复这个问题?

非常感谢!

这是预期的外观

这是当前的外观

英文:

I want to display two images above text in Jetpack Compose. To do this I create a row which has OnePlayer Composable inside of it.

  1. Box(
  2. modifier.fillMaxSize()
  3. ){
  4. Box(
  5. modifier
  6. .clip(RoundedCornerShape(10.dp))
  7. .fillMaxWidth(0.95f)
  8. .fillMaxHeight(0.15f)
  9. .background(Color.DarkGray)
  10. .align(Alignment.Center)
  11. ) {
  12. Row(
  13. horizontalArrangement = Arrangement.SpaceEvenly,
  14. modifier = Modifier.fillMaxSize()
  15. ) {
  16. OnePlayer(name = "Gabriel Clemens", id = R.drawable.gabriel_clemens)
  17. OnePlayer(name = "Martin Schindler", id = R.drawable._8111069
  18. )
  19. }
  20. }
  21. }

This is the OnePlayer Composable code

  1. @Composable
  2. fun OnePlayer(
  3. modifier: Modifier = Modifier,
  4. name: String,
  5. id: Int
  6. ) {
  7. Column(){
  8. Image(
  9. painter = painterResource(id = id),
  10. contentDescription = null,
  11. contentScale = ContentScale.FillWidth,
  12. modifier = Modifier.fillMaxWidth(0.5f)
  13. )
  14. Text(
  15. text = name,
  16. )
  17. }
  18. } }
  19. }

I want both images to be the same size and have a text below. When I use just text inside the second column I have both texts in the same size just like I want it to be. As soon as I add images the left image is bigger than the right one (see the image attatched). How do I fix this?

Thanks a lot

This is what its supposed to look like

This is what it looks like currently

答案1

得分: 0

您需要将Modifier传递给您的OnePlayer组合,并使用权重进行调整,

  1. @Composable
  2. fun OnePlayer(
  3. name: String,
  4. id: Int,
  5. modifier: Modifier = Modifier,
  6. ) {
  7. Column(
  8. modifier = modifier,
  9. horizontalAlignment = Alignment.CenterHorizontally,
  10. ) {
  11. Image(
  12. painter = painterResource(id = id),
  13. contentDescription = null,
  14. contentScale = ContentScale.Crop,
  15. modifier = Modifier.fillMaxWidth().weight(1f),
  16. )
  17. Text(
  18. text = name,
  19. maxLines = 1,
  20. )
  21. }
  22. }
  23. @Preview
  24. @Composable
  25. fun PreviewResult() {
  26. SampleTheme {
  27. Surface(
  28. color = MaterialTheme.colorScheme.background
  29. ) {
  30. Box(
  31. modifier = Modifier.fillMaxSize()
  32. ) {
  33. Box(
  34. Modifier
  35. .clip(RoundedCornerShape(10.dp))
  36. .fillMaxWidth(0.95f)
  37. .fillMaxHeight(0.15f)
  38. .background(Color.DarkGray)
  39. .align(Alignment.Center)
  40. ) {
  41. Row(
  42. horizontalArrangement = Arrangement.SpaceEvenly,
  43. modifier = Modifier.fillMaxSize()
  44. ) {
  45. OnePlayer(
  46. name = "Gabriel Clemens",
  47. id = R.drawable.ic_foggy,
  48. modifier = Modifier.weight(1f),
  49. )
  50. OnePlayer(
  51. name = "Martin Schindler",
  52. id = R.drawable.ic_heavy_rain,
  53. modifier = Modifier.weight(1f),
  54. )
  55. }
  56. }
  57. }
  58. }
  59. }
  60. }

[![Result][1]][1]

  1. <details>
  2. <summary>英文:</summary>
  3. You have to pass the `Modifier` to your `OnePlayer` composable and play with weights,
  4. @Composable
  5. fun OnePlayer(
  6. name: String,
  7. id: Int,
  8. modifier: Modifier = Modifier,
  9. ) {
  10. Column(
  11. modifier = modifier,
  12. horizontalAlignment = Alignment.CenterHorizontally,
  13. ) {
  14. Image(
  15. painter = painterResource(id = id),
  16. contentDescription = null,
  17. contentScale = ContentScale.Crop,
  18. modifier = Modifier.fillMaxWidth().weight(1f),
  19. )
  20. Text(
  21. text = name,
  22. maxLines = 1,
  23. )
  24. }
  25. }
  26. @Preview
  27. @Composable
  28. fun PreviewResult() {
  29. SampleTheme {
  30. Surface(
  31. color = MaterialTheme.colorScheme.background
  32. ) {
  33. Box(
  34. modifier = Modifier.fillMaxSize()
  35. ) {
  36. Box(
  37. Modifier
  38. .clip(RoundedCornerShape(10.dp))
  39. .fillMaxWidth(0.95f)
  40. .fillMaxHeight(0.15f)
  41. .background(Color.DarkGray)
  42. .align(Alignment.Center)
  43. ) {
  44. Row(
  45. horizontalArrangement = Arrangement.SpaceEvenly,
  46. modifier = Modifier.fillMaxSize()
  47. ) {
  48. OnePlayer(
  49. name = &quot;Gabriel Clemens&quot;,
  50. id = R.drawable.ic_foggy,
  51. modifier = Modifier.weight(1f),
  52. )
  53. OnePlayer(
  54. name = &quot;Martin Schindler&quot;,
  55. id = R.drawable.ic_heavy_rain,
  56. modifier = Modifier.weight(1f),
  57. )
  58. }
  59. }
  60. }
  61. }
  62. }
  63. }
  64. [![Result][1]][1]
  65. [1]: https://i.stack.imgur.com/pcFGV.png
  66. </details>

huangapple
  • 本文由 发表于 2023年7月23日 23:48:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/76749125.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定