简体   繁体   English

如果在 Compose 中溢出,则在右侧显示图标并在中间显示省略号文本

[英]Show Icon at Right Side and Ellipsis Middle Text If Overflow In Compose

I want two behavior.我想要两种行为。

  1. [Working] If middle text is short, then heart icon should be next to middle text and cart should be at end, see image. [工作] 如果中间文本很短,那么心形图标应该在中间文本旁边,购物车应该在最后,见图。

在此处输入图像描述

  1. [Not working] If middle text is large, then heart icon should stick beside carts left and middle text be ellipsis. [不起作用] 如果中间文本较大,则心形图标应贴在购物车左侧,中间文本为省略号。

在此处输入图像描述

Note: I have tried Modifier.weight(1f,fill = false) for second behaviour but then first broke.注意:我已经尝试了Modifier.weight(1f,fill = false)的第二个行为,但随后首先失败了。

code代码

 Row(
            modifier = Modifier.fillMaxSize()
        ) {
            Row(
                modifier = Modifier.wrapContentWidth()
            ) {
                Icon(Icons.Filled.Search,"")

                Spacer(modifier = Modifier.width(18.dp))

                Icon(Icons.Filled.Add,"")

                Spacer(modifier = Modifier.width(12.dp))

                Text(
                    text = "If text is long, then cart icon show at end with ellipsis text",
                    maxLines = 1,
                    modifier = Modifier
                        .weight(1f,fill = false)
                )
                Spacer(modifier = Modifier.width(12.dp))

                Icon(Icons.Filled.Favorite,"")
            }

            Row(Modifier.fillMaxWidth(),
            horizontalArrangement = Arrangement.End) {
                Icon(Icons.Filled.ShoppingCart,"")
            }
        }

You can wrap the Text and the Favorite Icon with a Row and apply to it the weight modifier to fill the available space.您可以用Row包裹Text和收藏夹Icon ,并对其应用weight修改器以填充可用空间。
Then assign weight(1f, fill = false) to the Text :然后将weight(1f, fill = false)分配给Text

    Row(
        modifier = Modifier.fillMaxWidth().background(Color.Yellow),
    ) {
        Icon(Icons.Filled.Search,"")
        Spacer(modifier = Modifier.width(18.dp))
        Icon(Icons.Filled.Add,"")

        Row(Modifier.weight(1f)) {
            Text(
                text = "If text is",
                maxLines = 1,
                modifier = Modifier
                    .weight(1f, fill = false)
                    .padding(8.dp),
                overflow = TextOverflow . Ellipsis
            )
            Icon(
                Icons.Filled.Favorite, "",
            )
        }

        Icon(Icons.Filled.ShoppingCart,"")
    }

在此处输入图像描述

You center component should use the "left space" with weight attribute您的中心组件应使用具有重量属性的“左侧空间”

    modifier = Modifier
        .weight(1f)
        .padding(8.dp),
    overflow = TextOverflow.Ellipsis

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

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