簡體   English   中英

將自定義字體添加到所有LinearLayout文本視圖

[英]Add custom font to all LinearLayout textviews

我以編程方式添加了“線性布局”和多個textview,並將自定義字體設置為文本,

我以波紋管的方式完成了它,並且效果很好。

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()

對於每個文本,這是應用自定義字體的一種繁重方法,我嘗試將自定義視圖設置為波紋管,但它並未將自定義字體設置為文本:

主要活動:

  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);
        }
    }

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

不必每次都從資產中創建字體,而是創建一個“ 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());

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

您已經創建了一個自定義TextView類,卻忘了實現它。

CustomTextView tv1 = new CustomTextView(this); 

TextView tv1 = new TextView(this); 

與所有其他TextView一樣

希望這可以幫助....

您不必為每個TextView創建一個Typeface 您可以執行以下操作: Typeface face=Typeface.createFromAsset(getAssets(),"BFantezy.ttf"); 之后,可以為每個TextView執行textview.setTypeface(face) 僅調用方法createFromAsset()一次,然后重用您獲得的對象。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM