简体   繁体   English

如何在 android Jetpack Compose 中创建自定义形状?

[英]how create custom shape in android jetpack compose?

How can I make a shape like this?我怎样才能做出这样的形状?

I see there two ways how I would've done this:我看到有两种方法可以做到这一点:

  1. You can create @Composable function with Canvases ( official guideline , article on Medium ), if you need to use tab of this folder-like shape如果您需要使用这种类似文件夹形状的选项卡,您可以使用 Canvases 创建 @Composable function( 官方指南Medium 上的文章
// Usage:
@Composable
fun Somewhere() {
  FolderLikeCard(
    topTailContent = {
      // for example, your tab will just be, without content, only for shape
      Box(modifier = Modifier.size(64.dp, 12.dp))
    },
    mainContent = {
      // main content
    }
  )
}


// Implementation:
@Composable
fun FolderLikeCard(
  topTailContent: @Composable () -> Unit,
  mainContent: @Composable () -> Unit
) {
  val cornerSize = 4.dp // for example
  Column {
    Row {
      Spacer(modifier = Modifier.weight(1f))
      Box {
        Canvas {
          TODO("draw with help of links above tab's outline (using drawArc maybe)")
        }
        Box(
          modifier = Modifier.padding(left = cornerSize, top = cornerSize, right = cornerSize),
          content = topTailContent
        )
      }
    }
    Box {
      Canvas {
        TODO("draw main part outline")
      }
      Box(
        modifier = Modifier.padding(left = cornerSize, bottom = cornerSize, right = cornerSize),
        content = mainContent
      )
    }
  }
}

+ I feel it can be refactored with help of Modifier.drawBehind method + 我觉得它可以在Modifier.drawBehind方法的帮助下重构

  1. Create an actual Shape for using it in Modifier.background(color = yourColor, shape = FolderLikeShape(tabHeight, tabWidth, ...)) method, link to the article that I read some time ago , + linked question helps , but I don't see how you can then put the content there so far, if you need it, then I hope the following commentators will help with this.创建一个实际的形状以在Modifier.background(color = yourColor, shape = FolderLikeShape(tabHeight, tabWidth, ...))方法中使用它, 链接到我前段时间阅读的文章+ 链接问题有帮助,但我不看看你怎么能把内容放在那里呢,如果你需要的话,那么我希望下面的评论员能帮助解决这个问题。

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

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