TextView使用实例
生活随笔
收集整理的這篇文章主要介紹了
TextView使用实例
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
第一次發文章好緊張哦,一定要我。
一、引用
1、TextView實戰之你真的懂我么?
2、Android TextView 添加下劃線的幾種方式
3、Android在一個TextView里顯示不同樣式的字體
4、盤點Android使用自定義字體遇到的坑
5、Android應用使用第三方字體
二、實例
1、完全靠xml布局實現的各式文字樣式(超鏈接,下劃線,滾動,陰影,拉伸,粗體斜體,圖片加載)
圖例: tv-1.pngxml:
<!--part 1--><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:background="@color/white"android:orientation="vertical"><TextViewstyle="@style/text_part_title_style"android:layout_margin="3dp"android:text="@string/textview_title_1" /><!--none|all|web|phone|mail|map--><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_margin="3dp"android:autoLink="web"android:text="www.baidu.com" /><!--代碼跑起來才知道--><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_margin="3dp"android:text="@string/textview_line" /><TextViewandroid:layout_width="100dp"android:layout_height="wrap_content"android:layout_margin="3dp"android:maxLength="8"android:text="測試長度1234566777" /><!--start,end,middle分別是在哪里省略,marquee是滾動,android4.x error--><TextViewandroid:layout_width="100dp"android:layout_height="wrap_content"android:layout_margin="3dp"android:ellipsize="marquee"android:focusable="true"android:focusableInTouchMode="true"android:marqueeRepeatLimit="marquee_forever"android:maxEms="8"android:singleLine="true"android:text="數據太長只能滾動看123456" /><TextViewandroid:layout_width="100dp"android:layout_height="wrap_content"android:layout_margin="3dp"android:singleLine="true"android:text="測試單行test1234567890" /><TextViewandroid:layout_width="100dp"android:layout_height="wrap_content"android:layout_margin="3dp"android:maxLines="2"android:text="測試最多2行test1234567890xxxxxx" /><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_margin="3dp"android:digits="123456"android:text="測試只能數字test1234567890xxxxxx" /><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_margin="3dp"android:hint="測試默認隱藏文字"android:textColorHint="@color/green" /><!--斜體在4.x上不行,要寫nomospace?? , 陰影得運行才看得見--><TextViewandroid:layout_width="match_parent"android:layout_height="wrap_content"android:layout_margin="3dp"android:background="@color/wx_blue"android:drawableLeft="@mipmap/android_label"android:gravity="center"android:minHeight="30dp"android:padding="10dp"android:scaleY="1.2"android:shadowColor="@color/red"android:shadowDx="15.0"android:shadowDy="5.0"android:shadowRadius="2.5"android:text="布局實現:大小,顏色,最低高度,居中,背景色,內距,拉伸_文字間隔,陰影,粗體斜體,加載圖片"android:textColor="@color/white"android:textSize="18sp"android:textStyle="italic|bold"android:typeface="monospace" /></LinearLayout>2、textview和string.xml的部分使用(格式化文字和string-array)
圖例: tv-2.pngxml:
<!--part 2--><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginTop="5dp"android:background="@color/white"android:orientation="vertical"><TextViewstyle="@style/text_part_title_style"android:layout_margin="3dp"android:text="@string/textview_title_2" /><TextViewandroid:id="@+id/tv_string"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_margin="3dp"android:text="@string/textview_string" /><TextViewandroid:id="@+id/tv_string_format"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_margin="3dp"android:text="@string/textview_string_format" /></LinearLayout>string.xml等:
<!--main--><string-array name="android_study_modules"><item>組件</item><item>控件</item><item>數據庫</item><item>通信</item><item>多媒體</item><item>文件</item><item>設備</item></string-array><string name="textview_string_format">測試StringFormat:StringArray有%d個數據=%s,可測試Float=%2f</string>代碼:
/*** 測試string.xml的使用* XLIFF,全稱叫 XML 本地化數據交換格式,英文全稱 XML Localization Interchange File Format* */private void testStringXml(){//normaltv_string.setText(R.string.textview_string);//superString[] array_string = getResources().getStringArray(R.array.android_study_modules);LogUtil.i("array_string.length="+array_string.length+",content="+ Arrays.toString(array_string));String format_string = getString(R.string.textview_string_format);LogUtil.i("format_string org = "+format_string);format_string = String.format(format_string,array_string.length,Arrays.toString(array_string),3.333f);LogUtil.i("format_string java code = "+format_string);tv_string_format.setText(format_string);format_string = getString(R.string.textview_string_format,array_string.length,Arrays.toString(array_string),3.333f);LogUtil.i("format_string android code = "+format_string);tv_string_format.setText(format_string);}3、代碼實現各種文字樣式(SpannableString的運用,文字內部超鏈接,下劃線,滾動,陰影,拉伸,粗體斜體,圖片加載,第三方字體,奇奇怪怪的文字)
圖例: tv-3.pngxml:
<!--part 3--><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginTop="5dp"android:background="@color/white"android:orientation="vertical"><TextViewstyle="@style/text_part_title_style"android:layout_margin="3dp"android:text="@string/textview_title_3" /><TextViewandroid:id="@+id/tv_code_italic"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_margin="3dp"android:text="代碼實現:斜體" /><TextViewandroid:id="@+id/tv_code_bottom_line"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_margin="3dp"android:text="代碼實現:下劃線" /><TextViewandroid:id="@+id/tv_code_autolink"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_margin="3dp"android:text="代碼實現:AutoLinkALL" /><TextViewandroid:id="@+id/tv_code_autolink2"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_margin="3dp"android:text="代碼實現:定制化AutoLink" /><TextViewandroid:id="@+id/tv_code_asset"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_margin="3dp"android:text="代碼實現:第三方asset字體應用" /><TextViewandroid:id="@+id/tv_code_fun_style"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_margin="3dp"android:text="代碼實現:亂七八糟,大小上下顏色各不同的文字,真的很好玩" /><TextViewandroid:id="@+id/tv_code_diff_style"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_margin="8dp"android:text="代碼實現:大小,顏色,最低高度,居中,背景色,內距,拉伸_文字間隔,陰影,粗體斜體,加載圖片" /></LinearLayout>代碼:
/*** 測試斜體* 貌似必須加上MONOSPACE,否則中文字無法斜體* */private void testItalic(){String aaa = tv_code_italic.getText().toString();/*方法1:全部是斜體粗體*/tv_code_italic.setTypeface(Typeface.MONOSPACE,Typeface.BOLD_ITALIC);/*方法2:部分斜體(中文字的話沒法斜體)*///SpannableString spn = new SpannableString(aaa);//what,start,end,flag//spn.setSpan(spn,0,3, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);//tv_code_italic.setText(spn);/*方法3:使用畫筆(中文字的話沒法斜體)*///tv_code_italic.getPaint().setFakeBoldText(true);}/*** 測試中劃線/下劃線* */private void testTxtLine(){/*方法1:paint : STRIKE_THRU_TEXT_FLAG=中劃線(刪除線) , UNDERLINE_TEXT_FLAG=下劃線*/tv_code_bottom_line.setPaintFlags(Paint.UNDERLINE_TEXT_FLAG);//getPaint().setFlags();tv_code_bottom_line.getPaint().setAntiAlias(true);//抗鋸齒/*方法2:html標簽類文字*///String htmlString = "<u>我是html下劃線</u>";//tv_code_bottom_line.setText(Html.fromHtml(htmlString));/*方法3:SpannablString類,一般用于某個文字下劃線*///String spanString = "我是spna文字,啦啦1234";//SpannableString content = new SpannableString(spanString);//content.setSpan(new UnderlineSpan(), 0, spanString.length(), 0);//tv_code_bottom_line.setText(content);}/*** 測試autoLink內容* none|web|phone|mail|map* */private void testAutoLink(){/*固定的 autolink all*/final String contact = "Email: xxxxxx@qq.com\n" +"Phone: 189111111111\n" +"Fax: +47-12345678\n" +"HTTP: http://www.baidu.com";tv_code_autolink.setAutoLinkMask(Linkify.ALL); // or set 'android:autoLink' in layout xmltv_code_autolink.setText(contact);/*定制的 autolink all*///將TextView的顯示文字設置為SpannableStringtv_code_autolink2.setText(getClickableSpan());//設置該句使文本的超連接起作用tv_code_autolink2.setMovementMethod(LinkMovementMethod.getInstance());}//設置超鏈接文字private SpannableString getClickableSpan() {SpannableString spanStr = new SpannableString("使用該軟件,即表示您同意該軟件的使用條款和隱私政策");//設置下劃線文字spanStr.setSpan(new UnderlineSpan(), 16, 20, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);//設置文字的單擊事件spanStr.setSpan(new ClickableSpan() {@Overridepublic void onClick(View widget) {//startActivity(new Intent(MainActivity.this, UsageActivity.class));ToastUtil.showShort(instance,"點擊了使用條款");}}, 16, 20, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);//設置文字的前景色spanStr.setSpan(new ForegroundColorSpan(Color.GREEN), 16, 20, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);//設置下劃線文字spanStr.setSpan(new UnderlineSpan(), 21, 25, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);//設置文字的單擊事件spanStr.setSpan(new ClickableSpan() {@Overridepublic void onClick(View widget) {//startActivity(new Intent(MainActivity.this, PrivacyActivity.class));ToastUtil.showShort(instance,"點擊了隱私政策");}}, 21, 25, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);//設置文字的前景色spanStr.setSpan(new ForegroundColorSpan(Color.GREEN), 21, 25, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);return spanStr;}/*** 測試第三方asset中字體* 如果全局引用第三方字體兩種比較好方式* 1、MyApplication通過反射定義此typeface,然后在theme中引用,然后套用此theme* 優化一點,可以通過注解+反射的方式,類似bufferfly框架中* 2、各種自定義控件中屬性使用新typeface* 3、單個使用慢慢設置...** 全局使用請看HelloApp.java這個Application文件* */private void testAssetFont(){//maybe exceptionTypeface ttf = Typeface.createFromAsset(getAssets(),"fonts/SIMKAI.TTF");tv_code_asset.setTypeface(ttf);}/*** 測試同一段文字顯示亂七八糟有趣的* 主要是SpannableString* */private void testFunnyTextView(){String funnyString = tv_code_fun_style.getText().toString();SpannableString spna = new SpannableString(funnyString);// 字體顏色spna.setSpan(new ForegroundColorSpan(Color.RED), 0, 2, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);// 背景色spna.setSpan(new BackgroundColorSpan(Color.RED), 2, 4, Spannable.SPAN_INCLUSIVE_INCLUSIVE);// 粗體spna.setSpan(new StyleSpan(Typeface.BOLD), 4, 6, Spannable.SPAN_INCLUSIVE_INCLUSIVE);// 字體大小spna.setSpan(new AbsoluteSizeSpan(50), 6, 8, Spannable.SPAN_INCLUSIVE_INCLUSIVE);// 下劃線spna.setSpan(new URLSpan(""), 8, 10, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);// 刪除線(中劃線)spna.setSpan(new StrikethroughSpan(), 10, 12, Spannable.SPAN_INCLUSIVE_INCLUSIVE);// 胖瘦字體spna.setSpan(new ScaleXSpan(2.0f), 12, 13, Spannable.SPAN_INCLUSIVE_INCLUSIVE);spna.setSpan(new ScaleXSpan(0.5f), 13, 14, Spannable.SPAN_INCLUSIVE_INCLUSIVE);// 顯示tv_code_fun_style.setText(spna);}/*** 測試用代碼輸出風格迥異的文字* 設置各種尺寸要用到sp,dp和px,最多是px* */private void testTextViewCode(){//設置背景色,其他:setBackgroundResource(resid),setBackground(drawable)tv_code_diff_style.setBackgroundColor(AppUtil.getColor(instance,R.color.wx_blue));//設置字體顏色tv_code_diff_style.setTextColor(Color.WHITE);//設置大小tv_code_diff_style.setTextSize(18f);//默認用的sp為單位,不需要修改//設置寬度:非固定高度的,設置match_parent等需要從他父控件的param開始設置//tv_code_diff_style.setWidth(100);//pixLinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.WRAP_CONTENT);//params.width = 100;params.height=100;params.weight=1;tv_code_diff_style.setLayoutParams(params);//設置居中tv_code_diff_style.setGravity(Gravity.CENTER);//設置最低高度tv_code_diff_style.setMinHeight(DisplayUtil.dip2px(instance,30f));//pix//設置內距tv_code_diff_style.setPadding(10,10,10,10);//設置拉伸_文字間隔tv_code_diff_style.setScaleY(1.2f);//setScaleX//設置陰影tv_code_diff_style.setShadowLayer(2.5f,15f,5f,Color.RED);//設置斜體粗體tv_code_diff_style.setTypeface(Typeface.MONOSPACE,Typeface.BOLD_ITALIC);//設置加載圖片Drawable drawable= getResources().getDrawable(R.mipmap.android_label);drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());//必有,不然不顯示tv_code_diff_style.setCompoundDrawables(drawable,null,null,null);}總結
以上是生活随笔為你收集整理的TextView使用实例的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: javascript之原型
- 下一篇: 洛谷P1456 Monkey King