【问题标题】:Multiple colored Activity Label element on Xamarin.AndroidXamarin.Android 上的多个彩色活动标签元素
【发布时间】:2023-03-12 20:54:01
【问题描述】:

我想知道是否可以让 Label 元素由两种不同的颜色组成。例如,我的标签是“MyLabel”,我希望“My”部分为红色,“Label”部分为蓝色。

我尝试使用本指南,但这种方法仅适用于一种颜色。 https://blog.qualtechsoftware.com/how-to-customize-the-title-android-with-xamarin

【问题讨论】:

    标签: c# android mobile xamarin xamarin.android


    【解决方案1】:

    查看 Android 文档中的 SpannableString

    http://developer.android.com/reference/android/text/SpannableString.html

        var textView = FindViewById<TextView>(Resource.Id.my_label);
        var span = new SpannableString("My Label");
    
        span.SetSpan(new ForegroundColorSpan(Color.Red), 0, 2, 0);  // "My" is red
        span.SetSpan(new ForegroundColorSpan(Color.Blue), 3, 8, 0); // "Label" is blue
        textView.SetText(span, TextView.BufferType.Spannable);
    

    【讨论】:

    • 对于从原生 android 开发迁移到 Xamarin 的人的完美简洁答案。谢谢!
    猜你喜欢
    • 2013-09-21
    • 1970-01-01
    • 2014-08-02
    • 2012-11-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多