Android編程心得-為TextView添加各種樣式
在開發過程中,發現有時候TextView需要特殊顯示的時候,需要特殊處理,以下是常見的一些功能要求:
1.為TextView 添加下劃線
TextView appVersion=(TextView) root.findViewById(R.id.appversion_value); appVersion.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG); appVersion.setText("Ver 1.2.5");
如上代碼這樣就為這樣一個代碼添加了下劃線
2.為TextView添加超鏈接
String blogsite="http://blog.csdn.net/yangqicong11"; SpannableString spblog = new SpannableString( blogsite); //設置超鏈接 spblog.setSpan( new URLSpan( blogsite ), 0 , blogsite.length() , Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); PersonalBlog=(TextView) root.findViewById(R.id.PersonalBlog_value); PersonalBlog.setText(spblog); PersonalBlog.setMovementMethod(LinkMovementMethod.getInstance());
這樣我們的TextView就成了超鏈接的形式,用戶點擊后可以直接調用瀏覽器跳轉到對應頁面
3.為TextView 添加背景高亮顯示
String tmp= "背景高亮"; SpannableString sp = new SpannableString( "背景高亮" ); //設置高亮樣式一 sp.setSpan( new BackgroundColorSpan(Color.BLUE), 0 , tmp.length() ,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); TextView appEditor=(TextView) root.findViewById(R.id.appEditor_value); appEditor.setText(sp);
這樣我們的TextView 背景就變成高亮了
4.為TextView 添加文字高亮顯示
String tmp= "文字高亮"; SpannableString sp = new SpannableString( "文字高亮" ); //設置高亮樣式二 sp.setSpan( new ForegroundColorSpan(Color.YELLOW), 0 , tmp.length() ,Spannable.SPAN_EXCLUSIVE_INCLUSIVE); TextView appEditor=(TextView) root.findViewById(R.id.appEditor_value); appEditor.setText(sp);
與上一個所不同的是 這里僅僅只是TextView中的文字產生了高亮的效果
5.為TextView添加加粗斜體顯示
String tmp= "設置斜體"; SpannableString sp = new SpannableString( "設置斜體" ); //設置斜體 sp.setSpan( new StyleSpan(android.graphics.Typeface.BOLD_ITALIC), 0 , tmp.length() , Spannable.SPAN_EXCLUSIVE_INCLUSIVE); TextView appEditor=(TextView) root.findViewById(R.id.appEditor_value); appEditor.setText(sp);
本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!