android:為TextView添加樣式——底線,顏色,設定連結樣式及前背景色

來源:互聯網
上載者:User

標籤:

實現底線及顏色設定:


public class AtActivity extends Activity {

LinearLayout ll;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        ll=(LinearLayout)findViewById(R.id.ll);
        TextView tv=new TextView(this);
        
        tv.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG);//底線
        tv.setText("使用代碼實現底線樣式");
        tv.setTextColor(Color.WHITE);
        ll.addView(tv);
        
        tv=new TextView(this);
        tv.setText(Html.fromHtml("<u>使用html實現底線樣式</u>"));
        ll.addView(tv);
 
    }
}

設定超連結樣式:預設的超連結是藍色的,我們現在設定成前景紅色


public class AtActivity extends Activity {

LinearLayout ll;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        ll=(LinearLayout)findViewById(R.id.ll);
        TextView tv=new TextView(this);
        
       
        tv.setText(Html.fromHtml("<a href=\"http://blog.csdn.net/CAIYUNFREEDOM\">自訂的超連結樣式</a>"));
        tv.setMovementMethod(LinkMovementMethod.getInstance());  
        CharSequence text  =  tv.getText();
        if (text instanceof Spannable){ 
          
               int  end  =  text.length();   
              Spannable sp  =  (Spannable)tv.getText();   
              URLSpan[] urls = sp.getSpans( 0 , end, URLSpan.class );   
             
              SpannableStringBuilder style = new  SpannableStringBuilder(text);   
              style.clearSpans(); // should clear old spans    
               for (URLSpan url : urls){     
                  URLSpan myURLSpan=   new  URLSpan(url.getURL());                        
             style.setSpan(myURLSpan,sp.getSpanStart(url),sp.getSpanEnd(url),Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);  
             style.setSpan(new ForegroundColorSpan(Color.RED), sp.getSpanStart(url), sp.getSpanEnd(url), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);//設定前景色彩為紅色
               } 
               tv.setText(style);   
        }
        
        ll.addView(tv);
          
        tv=new TextView(this);
        tv.setText(Html.fromHtml("<a href=\"http://blog.csdn.net/CAIYUNFREEDOM\">預設的超連結樣式</a>"));
        tv.setMovementMethod(LinkMovementMethod.getInstance());  
        ll.addView(tv);
 
    }
}

android:為TextView添加樣式——底線,顏色,設定連結樣式及前背景色

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.