1

我以编程方式添加线性布局和多个文本视图,并将自定义字体设置为文本,

我以下面的方式做到了,而且效果很好。

主要活动 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);
}}

自定义文本视图:

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

任何帮助将不胜感激,谢谢。

4

3 回答 3

3

与其每次都从资产中创建字体,不如创建一个“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());

您不再需要担心创建字体。如果它需要它,工厂将创建它。您可以添加类似的方法来处理您希望使用的任何其他字体。

于 2013-05-15T11:10:59.470 回答
2

您已经创建了一个自定义 TextView 类并忘记实现它.. 尝试

CustomTextView tv1 = new CustomTextView(this); 

代替

TextView tv1 = new TextView(this); 

与所有其他 TextView 一样

希望这可以帮助....

于 2013-05-15T11:07:40.533 回答
0

您不必Typeface为每个TextView. 你可以做类似的事情Typeface face=Typeface.createFromAsset(getAssets(),"BFantezy.ttf");,然后为每一个TextView你可以做的事情textview.setTypeface(face)。只调用createFromAsset()一次方法并重用你得到的对象。

于 2013-05-15T11:11:11.047 回答