简体   繁体   English

Android Jetpack Compose 的卡中的图像宽度显示不正确

[英]Image width is not showing properly in Card of Android Jetpack Compose

I am trying to make layout using Jetpack Compose .我正在尝试使用Jetpack Compose进行布局。

I want to show an Image and a Text in each Card , stacked vertically using Column widget我想在每个Card中显示一个Image和一个Text ,使用Column 小部件垂直堆叠

I could successfully wrote the code using Composable function but I am getting issues in last Card like below mentioned:我可以使用Composable function成功编写代码,但我在最后一张卡片中遇到问题,如下所述:

  • The Image width is not FULL图像宽度不完整
  • The TextView is not showing below Image TextView在图片下方显示

Code:代码:

    @Composable
fun Dashboard(name1: String, name2: String, name3: String) {
    Column(
        modifier =
        Modifier.background(color = Color(0, 255, 0, 255))
    ) {
        Card() {
            Column(modifier = Modifier.padding(10.dp)) {
                Image(painter = painterResource(id = R.drawable.img1),
                    contentDescription = "Image 1")
                Text(text = name1)
            }
        }
        Card() {
            Column(modifier = Modifier.padding(10.dp)) {
                Image(painter = painterResource(id = R.drawable.img2),
                    contentDescription = "Image 2")
                Text(text = name2)
            }
        }
        Card() {
            Column(modifier = Modifier.padding(10.dp)) {
                Image(painter = painterResource(id = R.drawable.img2),
                    contentDescription = "Image 3")
                Text(text = name3)
            }
        }
    }

}

Output: Output: 在此处输入图像描述

The Last Card's image (img2) is same as middle Card's image but is giving issue at last index only最后一张卡片的图像 (img2) 与中间卡片的图像相同,但仅在最后一个索引处出现问题

Can anyone help what is wrong in my Composable Function?谁能帮助我的可组合 Function 出了什么问题?

Reference :参考

I am following tutorials of The Hyper Coder community this one我正在关注 Hyper Coder 社区教程

This answer is based on Jetpack Compose Beta 3. By default Column { } is not scroll able, It lays out the contents in the available space on the screen.此答案基于 Jetpack Compose Beta 3。默认情况下, Column { }不可滚动,它将内容布置在屏幕上的可用空间中。

So whats happening with your last image is, its shirking its height which resulted in shirking its width to maintain the aspect ratio.因此,您的最后一张图像发生的情况是,它推卸了它的高度,这导致推卸了它的宽度以保持纵横比。 This resulted in the green background to show up.这导致出现绿色背景。 So making your top Column { } scrollable should fix this.因此,使您的顶部Column { }可滚动应该可以解决此问题。

Column(
    modifier =  Modifier
    .background(color = Color(0, 255, 0, 255))
    .verticalScroll(state = rememberScrollState(0))
) {
    Card { ... }
    Card { ... }
    Card { ... }
  }

UPDATE: Better solution to handle such scenarios is by using LazyColum which is much better in performance to handle such a specific use case更新:处理这种情况的更好解决方案是使用LazyColum ,它在处理这种特定用例时的性能要好得多

@Composable
fun Dashboard(images: List<ImageData>) {
  LazyColumn(
    modifier = Modifier.background(color = Color(0, 255, 0, 255))
  ) {
    images.forEach {
      item { ImageCard(image = it) }
    }
  }
}

@Composable
fun ImageCard(image: ImageData) {
  Card {
    Column(modifier = Modifier.padding(10.dp)) {
      Image(
        painter = painterResource(id = image.drawable),
        contentDescription = image.text
      )
      Text(text = image.text)
    }
  }
}

val images = listOf(
  ImageData("Image 1", R.drawable.img1),
  ImageData("Image 2", R.drawable.img2),
  ImageData("Image 3", R.drawable.img2)
)

data class ImageData(
  val text: String,
  @DrawableRes val drawable: Int
)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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