简体   繁体   English

如何在jetpack compose中使表面背景半透明,而不是内容?

[英]How to make a surface background half transparent in jetpack compose, but not the content?

I want to achieve this layout:我想实现这个布局:

在此处输入图像描述

In XML I would add an image in a relative layout with match_parent attributes, then a view with a black half-transparent background set to match_parent as well, then the content.在 XML 中,我将在具有 match_parent 属性的相对布局中添加一个图像,然后添加一个黑色半透明背景也设置为 match_parent 的视图,然后是内容。

In compose I made this composeable:在撰写中,我使这个可组合:

@Composable
fun ImageCover(resourceId: Int, alpha: Float = 0.5f, content: @Composable () -> Unit) {
    Box(modifier = Modifier.fillMaxSize()) {
        Image(
            painter = painterResource(id = resourceId),
            contentDescription = null,
            modifier = Modifier.fillMaxSize(),
            contentScale = ContentScale.Crop
        )
        Surface(
            color = Color.Black, modifier = Modifier
                .fillMaxSize()
                .alpha(alpha)
        ) {
            content()
        }
    }
}

But the problem is alpha is applied to the surface and its content.但问题是 alpha 应用于表面及其内容。 So no matter what I put in the content, even if it's another surface with a background, will also be half transparent.所以无论我放入什么内容,即使是另一个有背景的表面,也会是半透明的。 Here, for example, the two sentences and two components at the bottom will be half transparent as well.在这里,例如,底部的两个句子和两个组件也将是半透明的。

The background color of the Surface is based on the color attribute. Surface的背景颜色基于color属性。
Apply the alpha to the color property instead of the Modifier .alpha应用于color属性而不是Modifier

Something like:就像是:

   Surface(
        color = Color.Black.copy(alpha = 0.6f), 
        modifier = Modifier.fillMaxSize()
    ){ 
       //....
    } 

在此处输入图像描述

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

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