2013-05-15 67 views
1

我加線性佈局和多textviews編程和自定義字體設置爲文本,添加自定義字體到所有的LinearLayout textviews

我做到了在波紋管的方式和它的作品完美。

MainActivity 1:

public class MainActivity extends Activity { 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    LinearLayout ll = (LinearLayout)findViewById(R.id.linearLayout); 

     TextView tv = new TextView(this); 
     tv.setGravity(Gravity.RIGHT); 
     tv.setTextColor(Color.GREEN); 
     tv.setTextSize(40);  
     ll.addView(tv); 
     Typeface face4=Typeface.createFromAsset(getAssets(),"BFantezy.ttf");  
     tv.setTypeface(face4); 
     tv.setText(Html.fromHtml(getString(R.string.trip)));  

     ImageView divider = new ImageView(this); 
     LinearLayout.LayoutParams lp = 
     new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, 5); 
     lp.setMargins(10, 10, 10, 10); 
     divider.setLayoutParams(lp); 
     divider.setBackgroundColor(Color.BLUE); 
     ll.addView(divider); 

     TextView tv1 = new TextView(this);  
     tv1.setGravity(Gravity.RIGHT); 
     tv1.setTextSize(40); 
     ll.addView(tv1); 
     Typeface face1=Typeface.createFromAsset(getAssets(),"BFantezy.ttf");  
     tv1.setTypeface(face1); 
     tv1.setText(Html.fromHtml(getString(R.string.trip1))); 

     ImageView divider1 = new ImageView(this); 
     LinearLayout.LayoutParams lp1 = 
     new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, 5); 
     lp1.setMargins(10, 10, 10, 10); 
     divider1.setLayoutParams(lp1); 
     divider1.setBackgroundColor(Color.BLUE); 
     ll.addView(divider1); 

     TextView tv2 = new TextView(this); 
     tv2.setGravity(Gravity.RIGHT); 
     tv2.setTextSize(40); 
     ll.addView(tv2); 
     Typeface face2=Typeface.createFromAsset(getAssets(),"BFantezy.ttf");  
     tv2.setTypeface(face2); 
     tv2.setText(Html.fromHtml(getString(R.string.trip2))); 

     ImageView divider3 = new ImageView(this); 
     LinearLayout.LayoutParams lp3 = 
     new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, 5); 
     lp3.setMargins(10, 10, 10, 10); 
     divider3.setLayoutParams(lp3); 
     divider3.setBackgroundColor(Color.BLUE); 
     ll.addView(divider3);  
         } 
        } 

但召回

Typeface.createFromAsset(getAssets() 

與每個文本,它的重方式應用自定義字體, 我試圖使自定義視圖婁但它不設置自定義的字體文本:

MainActivity:

public class MainActivity extends Activity { 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    LinearLayout ll = (LinearLayout)findViewById(R.id.linearLayout); 

    // add text view 
    TextView tv = new TextView(this); 
    tv.setGravity(Gravity.RIGHT); 
    tv.setTextColor(Color.GREEN); 
    tv.setTextSize(40);  
    ll.addView(tv); 
    tv.setText(Html.fromHtml(getString(R.string.trip)));  

    ImageView divider = new ImageView(this); 
    LinearLayout.LayoutParams lp = 
    new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, 5); 
    lp.setMargins(10, 10, 10, 10); 
    divider.setLayoutParams(lp); 
    divider.setBackgroundColor(Color.BLUE); 
    ll.addView(divider); 


    TextView tv1 = new TextView(this);  
    tv1.setGravity(Gravity.RIGHT); 
    tv1.setTextSize(40); 
    ll.addView(tv1); 
    tv1.setText(Html.fromHtml(getString(R.string.trip1))); 

    ImageView divider1 = new ImageView(this); 
    LinearLayout.LayoutParams lp1 = 
    new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, 5); 
    lp1.setMargins(10, 10, 10, 10); 
    divider1.setLayoutParams(lp1); 
    divider1.setBackgroundColor(Color.BLUE); 
    ll.addView(divider1); 


    TextView tv2 = new TextView(this); 
    tv2.setGravity(Gravity.RIGHT); 
    tv2.setTextSize(40); 
    ll.addView(tv2); 
    tv2.setText(Html.fromHtml(getString(R.string.trip2))); 

    ImageView divider3 = new ImageView(this); 
    LinearLayout.LayoutParams lp3 = 
    new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, 5); 
    lp3.setMargins(10, 10, 10, 10); 
    divider3.setLayoutParams(lp3); 
    divider3.setBackgroundColor(Color.BLUE); 
    ll.addView(divider3); 
}} 

CustomTextView:

public class CustomTextView extends TextView { 

    public CustomTextView(Context context, AttributeSet attrs, int defStyle) { 
     super(context, attrs, defStyle); 
     init(); 
    } 

    public CustomTextView(Context context, AttributeSet attrs) { 
     //call the constructor which has the complete definition 
     this(context, attrs, 0); 
     init(); 
    } 

    public CustomTextView(Context context) { 
     super(context); 
     init(); 
    } 

    private void init() { 

      Typeface tf = Typeface.createFromAsset(getContext().getAssets(), "BFantezy.ttf"); 
      setTypeface(tf); 
     } 
    } 

任何幫助將不勝感激,謝謝。

回答

3

而不是建立在T字型他每次都有資產,創建一個「FontFactory」並重用同一個。

public class FontFactory { 

private static Typeface t1; 

public static Typeface getBFantezy(Context c) { 
    if (t1 == null) { 
     t1 = Typeface.createFromAsset(c.getAssets(), "BFantezy.ttf"); 
    } 
    return t1; 
} 


private static Typeface t2; 

public static Typeface getOtherFont(Context c) { 
    if (t2 == null) { 
     t2 = Typeface.createFromAsset(c.getAssets(), "OtherFont.ttf"); 
    } 
    return t2; 
} 
} 

然後用它在你的代碼,你只想:

tv1.setTypeface(FontFactory.getBFantezy(getContext()); 

您再也不必擔心創建字樣。如果需要它,工廠將創建它。您可以添加類似的方法來處理您希望使用的任何其他字體。

+0

如何將FontFactory應用到我的代碼,如果這解決了爲什麼要創建自定義視圖,所以我可以將它應用到我寫的第一個代碼(MainActivity 1),謝謝你的幫助,因爲我是新的android – androidqq6

+0

FontFactory只是一個標準類提供字體對象並在必要時創建它們。我修改了我的答案以顯示使用情況。 – Kuffs

+0

我像這樣添加它:TextView tv = new TextView(this); \t tv.setGravity(Gravity.RIGHT); \t tv.setTextColor(Color。綠色); \t tv.setTextSize(40); \t tv.setTypeface(FontFactory.getBFantezy(getContext())); \t ll.addView(tv); \t \t \t tv.setText(Html.fromHtml(getString(R.string.trip))); \t但我給了紅色標記錯誤:getContext錯誤是:1 - 創建方法getContext(),或2 - 更改爲GetBaseContext – androidqq6

2

您已經創建了一個自定義的TextView類,忘了實現它.. 嘗試

CustomTextView tv1 = new CustomTextView(this); 

TextView tv1 = new TextView(this); 

instaed同樣適用於所有其他的TextView的

希望這有助於.. ..

0

您不必爲每個TextView一個Typeface。你可以這樣做Typeface face=Typeface.createFromAsset(getAssets(),"BFantezy.ttf");,之後每個TextView你可以做textview.setTypeface(face)。只調用一次方法createFromAsset()並重用您獲得的對象。