2011-01-12 76 views

回答

18

最簡單的解決辦法可能是創建一個自定義UnderLineTextView組件從TextView的派生,重載的setText(),並設置整個文本強調,這樣的事情(從你提到的鏈接中加下劃線代碼):

@Override 
public void setText(CharSequence text, BufferType type) { 
    // code to check text for null omitted 
    SpannableString content = new SpannableString(text); 
    content.setSpan(new UnderlineSpan(), 0, text.length(), 0); 
    super.setText(content, BufferType.SPANNABLE); 

} 

然後它只是一個ma在佈局中使用新組件並像往常一樣設置文本。其餘部分將自動處理。

更多關於自定義組件的信息: http://developer.android.com/guide/topics/ui/custom-components.html

14

可以下劃線的文本在Html.fromHtml(String source)

例子:

textView.setText(Html.fromHtml("this is <u>underlined</u> text")); 
+0

最簡單的解決方案恕我直言。 – gnac 2011-08-31 06:56:19

+0

這不會起作用,因爲'`和``標籤的開啓括號不會被轉義。有關正確的示例,請參閱http://stackoverflow.com/a/9955051/425183 – Ognyan 2012-03-31 10:25:00

+0

確實是最簡單的解決方案,txs – Hubert 2012-06-08 08:15:11

1

您可以通過/res/values/string.xml文件,如果你喜歡也做到這一點:例如,在/res/values/string.xml你可以添加類似的條目:

<string name="createAccount"><u>Create Account</u></string> 

,然後在活動的onCreate(捆綁savedInstanceState)方法,你能添加以下代碼,以使「創建帳戶」>出現在你的設置的UI下劃線您在/ res/layout /的xml文件中爲您的活動定義的createAccountText TextView:

TextView createAccountText = (TextView) findViewById(R.id.createAccountText); 
Resources res = getResources(); 
CharSequence styledText = res.getText(R.string.createAccount); 
createAccountText.setText(styledText, TextView.BufferType.SPANNABLE); 

丹尼·雷明頓 - MacroSolve

0
SpannableString content = new SpannableString(name); 
content.setSpan(new UnderlineSpan(), 0, name.length(), 0); 
geometrical_textview.setText(content, TextView.BufferType.SPANNABLE);