2016-11-11 80 views
0

我目前正在創建的TextView這樣的:如何用此特性以編程方式創建TextView屬性?

LinearLayout myLayout = (LinearLayout) activity.findViewById(R.id.ll1); 
    LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT); 

for(int l=0; l<4; l++) 
    { 
     pairs[l] = new TextView(context); 
     pairs[l].setTextSize(TypedValue.COMPLEX_UNIT_DIP, 16); 
     pairs[l].setLayoutParams(lp); 
     pairs[l].setId(l); 
     pairs[l].setText("asd"); 
     myLayout.addView(pairs[l]); 
    } 

現在我想這個屬性設置爲這一切的TextView的:

  • 的FontFamily:草書
  • SetTextSize不是SP,但DP(已解決)
  • SetGravity:central_horizo​​ntal(RESOLVED)

我無法找到一種方法來設置這些屬性,當我編程創建一個TextView,我該怎麼做?

+2

使用佈局PARAMS調整這些 –

+0

我做到了用於調整重力和高度(wrap_content),但最後兩個屬性如何? – GMX

回答

1

爲了改變一個TextView的字體家族使用setTypeface

例如:

Typeface tf = Typeface.create("cursive", Typeface.NORMAL); 
for(int l=0; l<4; l++) 
{ 
    pairs[l] = new TextView(context); 
    pairs[l].setTypeface(tf); 
    ... 
} 

此外,還可能感興趣的:How to change fontFamily of TextView in Android

0

可以使用字樣對象設置字體。

TextView tv = new TextView(context); 
Typeface face = Typeface.createFromAsset(getAssets(), 
     "fonts/filename.ttf"); 
tv.setTypeface(face); 

將字體放入項目的assets文件夾中。

+0

不能使用這個,因爲我沒有「Cursive」文件.TFF – GMX

相關問題