如何在运行时设置Xamarin Android ForegroundColorsPan.ForegroundColor属性?

发布于 2025-01-30 05:50:46 字数 1300 浏览 2 评论 0原文

Xamarin Android ForegroundColorspan.ForegroundColor属性已阅读。

如果要动态更改跨度的颜色,我们如何在运行时设置此?

更新

为了提供更多信息,我正在尝试重新创建Android文档中给出的示例。以下代码在Java中,但是由于Xamarin Android中跨度颜色属性的读取性质,该代码如何在Xamarin中重新创建?

https://develoveler.android.com/guide.com/guide/topics/topics/text/spans

public class MainActivity extends AppCompatActivity {

    private BulletPointSpan bulletSpan = new BulletPointSpan(Color.RED);

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        ...
        SpannableString spannable = new SpannableString("Text is spantastic");
        // setting the span to the bulletSpan field
        spannable.setSpan(bulletSpan, 0, 4, Spanned.SPAN_INCLUSIVE_INCLUSIVE);
        styledText.setText(spannable);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // change the color of our mutable span
                bulletSpan.setColor(Color.GRAY);
                // color won’t be changed until invalidate is called
                styledText.invalidate();
            }
        });
    }
}

The Xamarin Android ForegroundColorSpan.ForegroundColor Property is readonly.

How can we then set this at runtime if we want to dynamically change the color of a span?

UPDATED

To provide more information, I'm trying to recreate the example given in Android documentation. The below code is in Java, but due to the read only nature of the span's color property in Xamarin Android, how can this code be recreated in Xamarin?

https://developer.android.com/guide/topics/text/spans

public class MainActivity extends AppCompatActivity {

    private BulletPointSpan bulletSpan = new BulletPointSpan(Color.RED);

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        ...
        SpannableString spannable = new SpannableString("Text is spantastic");
        // setting the span to the bulletSpan field
        spannable.setSpan(bulletSpan, 0, 4, Spanned.SPAN_INCLUSIVE_INCLUSIVE);
        styledText.setText(spannable);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // change the color of our mutable span
                bulletSpan.setColor(Color.GRAY);
                // color won’t be changed until invalidate is called
                styledText.invalidate();
            }
        });
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文