簡體   English   中英

在Android小部件中以編程方式設置文本樣式的方法?

[英]Way to set text style programmatically in Android widget?

我看到有一個TextAppearanceSpan可用,但沒有用法示例。 我只想使文本變為粗體,而使其他所有內容保持不變-也許有一種更簡單的方法可以以編程方式執行此操作?

您需要做的是在res / values中創建一個xml文件,並編寫如下內容:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="header">
        /** here goes the style */
    </style>
</resources>

然后,您所需要做的就是將生成的R.style.header傳遞給TextAppearanceSpan構造函數。

http://developer.android.com/guide/appendix/faq/commontasks.html#selectingtext中有記錄,使用Spannable查看第二種方法。

特別

// Get our EditText object.
EditText vw = (EditText)findViewById(R.id.text); // or new etc

// Set the EditText's text.
vw.setText("Italic, highlighted, bold.");

// Get the EditText's internal text storage
Spannable str = vw.getText();

// Create our span sections, and assign a format to each.
str.setSpan(new StyleSpan(android.graphics.Typeface.ITALIC), 0, 7, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
str.setSpan(new BackgroundColorSpan(0xFFFFFF00), 8, 19, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
str.setSpan(new StyleSpan(android.graphics.Typeface.BOLD), 21, str.length() - 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM