android動態添加自定義TextView


// 設置背景圖
textView.setBackgroundResource(R.drawable.block_text_backgroumg);
// 設置背景透明度
textView.getBackground().setAlpha(150);
// 設定text內容為Html格式
textView.setText(Html.fromHtml(rsultText));
// 設定為可以scroll的textView
textView.setMovementMethod(ScrollingMovementMethod.getInstance());
// 設定text內容與邊框的距離
textView.setPadding(6, 6, 6, 6);
// 添加textView到Layout
mLytMain.addView(textView, textParams);

注意點:
1.----------------------------------
因為在android中TextView是沒有邊框的,為了添加邊框效果,有如下兩種方案,
1. 重寫TextView類  2.利用.9.png圖像來制作一個有邊框的背景。(上邊的例子使用了第二種方法)
參考網址:
Android學習系列(4)--App自適應draw9patch不失真背景
http://www.cnblogs.com/qianxudetianxia/archive/2011/04/17/2017591.html
Android Nine Patch圖片及按鈕背景
http://www.cnblogs.com/feisky/archive/2010/01/16/1649502.html

2.----------------------------------
在上面的例子中雖然已經設定為可以滾動的文本,但不會有滾動條顯示,這是可以借助ScrollView。
參考網址:http://wangjun.easymorse.com/?p=255

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent" android:layout_height="fill_parent"
    android:scrollbars="vertical" android:fadingEdge="vertical">
    <TextView android:layout_width="fill_parent"
        android:layout_height="wrap_content" android:id="@+id/description1"
        android:textColor="#071907" android:paddingTop="5dip" />
</ScrollView>

 


例外一種實現方式也是在布局文件中。android:scrollbars="vertical"
**在動態創建的時候有一個方法:setVerticalScrollBarEnabled,可在測試時沒有達到想要的效果。
3.----------------------------------
開發的過程中遇到一個問題,默認TextView的最后一行不顯示,而且左邊有很大一塊也無法顯示。
查到如下網址:http://znwa1m5y.blog.sohu.com/202620579.html
他是在布局文件中設定而非添加,解決辦法是將 TextView中  android:layout_width="fill_parent" 改為 wrap_content。
我試着在動態添加時在LayoutParams設定為wrap_content,可是沒有達到效果。
通過textView.setPadding(6, 6, 6, 6);解決
4.----------------------------------
我們還可以給文本框設定單行顯示和多行顯示的最大行數
android:singleLine="true"       <!--實現單行 --> (默認即為多行)
android:maxLines="15"            <!--最多不超過15行 -->
5.----------------------------------
關於EditText的多行文本顯示
在Android開發中,多行文本框EditText的默認顯示方式是居中,那怎么讓它從第一行開始顯示呢?
只需要在EditText的屬性中加上 android:gravity=”top”  即可。
或者通過編程的方式動態實現:
private EditText body;
body=(EditText)findViewById(R.id.main_body);
body.setGravity(Gravity.TOP);
其中函數setGravity()的參數是一個int,常見的可選值為 Gravity.TOP,Gravity.BOTTOM,
Gravity.LEFT和Gravity.RIGHT 。
6.----------------------------------
在html中設定的字體大小,在TextView中顯示時無效!!!
查資料得知:<font size=3> 數值設定從1到7。另外還有一種是在控件中通過style屬性來設定字體大小,
測試了一下<p style="font-size:20pt">測試字體大小</p>也是沒有作用,那要如何設置呢。
想不到好的辦法,只好解析html語言,返回size,通過setTextSize()來進行設定。
7.----------------------------------
如果不是通過html,而要動態設定textview中顯示的字體為粗體和斜體,要如何做呢?
text=new TextView(this);

// 設置字型為默認,正常字體
text.setTypeface(Typeface.DEFAULT,Typeface.NORMAL);
// 設置字型為默認粗體,粗體字體
text.setTypeface(Typeface.DEFAULT_BOLD, Typeface.BOLD);
// 設置字型為等寬字型,斜體字體
text.setTypeface(Typeface.MONOSPACE,Typeface.ITALIC);
// 設置字型為等寬字型,粗斜體字體
text.setTypeface(Typeface.MONOSPACE,Typeface.BOLD_ITALIC);
// 針對中文仿“粗體”
text.setTypeface(Typeface.DEFAULT_BOLD, Typeface.BOLD);
//使用TextPaint的仿“粗體”設置setFakeBoldText為true。目前還無法支持仿“斜體”方法
TextPaint tp = chinese.getPaint();
tp.setFakeBoldText(true);
// 自定義字體
//字體MgOpenCosmeticaBold.ttf放置於assets/font/路徑下
Typeface typeface=Typeface.createFromAsset(getAssets(),"font/MgOpenCosmeticaBold.ttf");
text.setTypeface(typeface);


8.----------------------------------
Android中TextVIew一些屬性

android:layout_gravity="center_vertical"
設置控件顯示的位置:默認top,這里居中顯示,還有bottom

android:hint="請輸入數字!"
設置顯示在空間上的提示信息

android:numeric="integer"
設置只能輸入整數,如果是小數則是:decimal

android:singleLine="true"
設置單行輸入,一旦設置為true,則文字不會自動換行。

android:password="true"
設置只能輸入密碼

android:gravity="top"
EditText設置,這一行就可以讓光標處於第一行了,若不設置默認就居中
TextView則在最頂上

android:textColor = "#ff8c00"
字體顏色

android:textStyle="bold"
字體,bold, italic, bolditalic

android:textSize="20dip"
大小

android:capitalize = "characters"
以大寫字母寫

android:textAlign="center"
EditText沒有這個屬性,但TextView有

android:autoText:自動拼寫幫助

android:editable:是否可編輯

android:textColorHighlight="#cccccc"
被選中文字的底色,默認為藍色

android:textColorHint="#ffff00"
設置提示信息文字的顏色,默認為灰色

android:textScaleX="1.5"
控制字與字之間的間距

android:typeface="monospace"
字型,normal, sans, serif, monospace

android:background="@null"
空間背景,這里沒有,指透明,將EditText自定義的背景去掉

android:imeOptions="actionDone"
設置軟鍵盤的Enter鍵

android:layout_weight="1"
權重,控制控件之間的地位,在控制控件顯示的大小時蠻有用的。

android:textAppearance="?android:attr/textAppearanceLargeInverse"
文字外觀,這里引用的是系統自帶的一個外觀,?表示系統是否有這種外觀,否則使用默認的外觀。不知道這樣理解對不對?


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM