简体   繁体   English

如何在元素底部添加高程? 喷气背包组成

[英]How to add elevation just in the bottom of an element? Jetpack Compose

I'm trying to add some elevation or shadow just in the bottom of an element.我试图在元素的底部添加一些高程或阴影。 For this element I'm using a Tab composable.对于这个元素,我使用 Tab 可组合。 How can I add this elevation/shadow just at the bottom as the image attached?如何在附加图像的底部添加此高程/阴影?

在此处输入图像描述

If you can see, the Text Field element has shadow around.如果您可以看到,Text Field 元素周围有阴影。 For this element I add对于这个元素,我添加

shape = 8.dp,
elevation = 6.dp

Inside a Surface element.在 Surface 元素内部。 But I can't use this same attributes in a Tab composable, besides, the shape and elevation adds around all the component, but I just want the "shadow" at the bottom of the Users and Pending users tab.但是我不能在 Tab 可组合中使用相同的属性,此外,形状和高度会添加到所有组件周围,但我只想要“用户”和“待定用户”选项卡底部的“阴影”。

For this implementation I'm using Jetpack Compose对于这个实现,我使用的是 Jetpack Compose

The way your are supposed to add elevation and shadows to elements in Compose is using Surfaces as containers for other content.您应该在 Compose 中为元素添加高程和阴影的方式是使用Surfaces作为其他内容的容器。

From Surface docs来自Surface文档

Material surface is the central metaphor in material design.材料表面是材料设计的核心隐喻。 Each surface exists at a given elevation, which influences how that piece of surface visually relates to other surfaces and how that surface is modified by tonal variance.每个表面都存在于给定的高度,这会影响该表面如何在视觉上与其他表面相关,以及该表面如何通过色调变化进行修改。

So to add shadow to a tab layout you could do something like this因此,要将阴影添加到选项卡布局中,您可以执行以下操作

Surface(
    shadowElevation = 9.dp, // play with the elevation values
) {
    Column {
        TabRow(...) // your current TabRow content
    }
}

If the above does not provide the desired shadow effect (I needed a stronger shadow) and since you have a rectangular shape anyway you could use a linear gradient instead.如果上面没有提供所需的阴影效果(我需要一个更强的阴影),并且因为你有一个矩形形状,你可以使用线性渐变来代替。

Write a @Composable function to make the "shadow"编写一个@Composable函数来制作“影子”

@Composable
fun BottomShadow(alpha: Float = 0.1f, height: Dp = 8.dp) {
    Box(modifier = Modifier
        .fillMaxWidth()
        .height(height)
        .background(
            brush = Brush.verticalGradient(
                colors = listOf(
                    Color.Black.copy(alpha = alpha),
                    Color.Transparent,
                )
            )
        )
    )
}

Then use it in your existing Compose layout然后在现有的 Compose 布局中使用它

Column {
    TabRow(...) // your current TabRow content

    BottomShadow(alpha = .15f, height = 8.dp) // use higher alpha for a stronger shadow
}

You can use the modifier shadow on your TabRow.您可以在 TabRow 上使用修饰符shadow For example, to add the default top app bar elevation you could do:例如,要添加默认的顶部应用栏高度,您可以执行以下操作:

TabRow(
    modifier = modifier.shadow(AppBarDefaults.TopAppBarElevation),
    ...
)

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

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