【问题标题】:Horizontal Stack labels in StackLayout that word warpStackLayout 中的水平堆栈标签,字扭曲
【发布时间】:2018-01-27 06:09:26
【问题描述】:

我正在编写一个 Xamarin.Forms 社交网络应用程序,并且正在努力做到这一点,因此当我在一行上有多个标签时,它们会根据需要占用尽可能多的空间,然后是下一个标签的文本等等。

这是我尝试的代码

StackLayout bodyLayout = new StackLayout
{
    Orientation = StackOrientation.Horizontal, HorizontalOptions = LayoutOptions.Fill 
};

Label word1 = new Label() { Text = "hey ppl you should follow" };
bodyLayout.Children.Add(word1);
Label word2 = new Label() { Text = "@SOCIALNETWORK" };
bodyLayout.Children.Add(word2);
Label word3 = new Label() { Text = "they are cool" };
bodyLayout.Children.Add(word3);

结果是这样的

我希望它打印出来

嘿,你应该关注@SOCIALNETWORK 他们很酷

当它到达屏幕的末尾时,它会进入下一行

【问题讨论】:

  • 您尝试过所有可能的LayoutOptions 选项吗?如果您使用 xaml 文件并使用 xamarin 预览器,这可能会有所帮助
  • 您需要使用 3 个标签是否有特殊原因?
  • @sme 这将用于添加对主题标签的支持,因此我可以拆分一个字符串以查找主题标签并使那个单词(主题标签)可点击
  • @Daniel 好的,现在说得通了。我认为创建自定义标签可能更容易,然后为每个平台使用自定义渲染器,以便能够单击标签中的给定子字符串。使用本机 Xamarin Forms 控件和布局,我认为没有一种方法可以满足您的需求,因此在任何情况下都需要自定义渲染器

标签: c# xamarin xamarin.forms


【解决方案1】:

尝试使用 Label.FormattedText 和 Span。有一些限制(最值得注意的是,Span 上没有可绑定的属性),但应该可以满足您的需求:

Label line = new Label()
{
    FormattedText = new FormattedString()
    {
        Spans =
        {
            new Span() { Text = "hey ppl you should follow" },
            new Span() { Text = "@SOCIALNETWORK" },
            new Span() { Text = "they are cool" },
        }
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-11-16
    • 2021-05-02
    • 2016-07-03
    • 2020-10-19
    • 2021-06-28
    • 2021-11-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多