2013-05-08 2 views
0

Один из моих приложений деятельности включают в себя много TextView я хочу установить несколько пользовательских шрифтов для этого TextViews,несколько пользовательских шрифтов для многократного TextView

Я попытался следующий код, но он не работает, чтобы настроить TextView,

любая помощь будет оценена

public class Text extends Activity { 
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     TextView tv2=(TextView)findViewById(R.id.text2); 
     tv2.setText(Html.fromHtml(getString(R.string.text_2))); 

     TextView tv3=(TextView)findViewById(R.id.text3); 
     tv3.setText(Html.fromHtml(getString(R.string.text_3))); 

     TextView tv4=(TextView)findViewById(R.id.text4); 
     tv4.setText(Html.fromHtml(getString(R.string.text_4))); 

     TextView tv5=(TextView)findViewById(R.id.text5); 
     tv5.setText(Html.fromHtml(getString(R.string.text_5))); 
    } 


    class MyTextView extends TextView { 
     private String TextView; 

     public MyTextView(Context context,int string) { 
      super(context); 
     } 

     public MyTextView(Context context) { 
      super(context); 
     } 

     public void setTypeface(Typeface tf, int string) { 
      if (TextView == "tv2,tv4") { 
       super.setTypeface(Typeface.createFromAsset(getContext().getAssets(), 
        "BFantezy.ttf")); 
      } 
      else if (TextView == "tv3,tv5") { 
       super.setTypeface(Typeface.createFromAsset(getContext().getAssets(), 
        "RoseNewB.ttf")); 
      } 
     } 
    } 
} 

main.xml:

<?xml version="1.0" encoding="utf-8"?> 
    <ScrollView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content"> 

    <LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:orientation="vertical"> 

     <TextView 
      android:id="@+id/text2" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:textSize="25sp"/> 

     <View 
     android:layout_width="fill_parent" 
     android:layout_height="2dp"/> 


     <TextView 
      android:id="@+id/text3" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:textSize="25sp"/> 

     <View 
     android:layout_width="fill_parent" 
     android:layout_height="2dp"/> 

     <TextView 
      android:id="@+id/text4" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content"    
      android:textSize="25sp"/> 

     <View 
     android:layout_width="fill_parent" 
     android:layout_height="2dp"/> 

     <TextView 
      android:id="@+id/text5" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content"    
      android:textSize="25sp"/> 

    </LinearLayout> 
</ScrollView> 
+2

if (TextView == "tv2, tv4") ... что это! – stinepike

+0

где вы используете MyTextView, вы только что определили этот класс. – Raghunandan

+3

Я думаю, что название «String» TextView является одним из худших зверств, которые я видел за долгое время. – kcoppock

ответ

0

Вы сравниваете его неправильно .. Сделайте это вместо того, чтобы

if (getId() == R.id.text2)          
      super.setTypeface(Typeface.createFromAsset(getContext().getAssets(), 
       "BFantezy.ttf")); 

и создать TextView с помощью MyTextView.

И вот это сравнить все.

+0

мой дорогой, пожалуйста, как я могу добавить шрифт и создать текстовое представление одним методом. – androidqq6

+0

в вашем методе settypface пользовательского текстового вида используйте сравнение, которое я написал в ответ. И создайте textview с использованием пользовательского текстового представления, а не обычного текстового изображения – stinepike

+0

, что означает: TextView tv4 = (TextView) findViewById (R.id.text4); tv4.setText (Html.fromHtml (getString (R.string.text_4))); super.setTypeface (Typeface.createFromAsset (getContext(). GetAssets(), "BFantezy.ttf")); – androidqq6

0

Вы создаете тот же шрифт каждый раз, когда вы его устанавливаете.

super.setTypeface(Typeface.createFromAsset(getContext().getAssets(), "BFantezy.ttf")); 

Просто создайте его один раз и повторно используйте его снова и снова. Этот вызов тяжелый, и его выполнение 40 раз, несомненно, увеличит время загрузки.

public class Text extends Activity {  
    /** Called when the activity is first created. */ 
    private Static Typeface bf = Typeface.createFromAsset(getContext().getAssets(), 
      "BFantezy.ttf"); 
    private Static Typeface rn = Typeface.createFromAsset(getContext().getAssets(), 
      "RoseNewB.ttf") 

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

     TextView tv2=(TextView)findViewById(R.id.text2); 
     tv2.setText(Html.fromHtml(getString(R.string.text_2))); 
     tv2.setTypeface(bf); 

     TextView tv3=(TextView)findViewById(R.id.text3); 
     tv3.setText(Html.fromHtml(getString(R.string.text_3))); 
     tv3.setTypeface(rn); 

     TextView tv4=(TextView)findViewById(R.id.text4); 
     tv4.setText(Html.fromHtml(getString(R.string.text_4))); 
     tv4.setTypeface(bf); 

     TextView tv5=(TextView)findViewById(R.id.text5); 
     tv5.setText(Html.fromHtml(getString(R.string.text_5)));} 
     tv5.setTypeface(rn); 
    ... 

Пользовательский TextView является излишним для этой цели. Вы можете удалить код ниже.

class MyTextView extends TextView {  
    private String TextView; 
    private Static Typeface bf = Typeface.createFromAsset(getContext().getAssets(), 
      "BFantezy.ttf"); 
    private Static Typeface rn = Typeface.createFromAsset(getContext().getAssets(), 
      "RoseNewB.ttf") 

    public MyTextView(Context context,int string) { 
     super(context); 
     updateTypeface(); 
    } 

    public MyTextView(Context context) {  
     super(context);  
     updateTypeface(); 
    } 

    private void updateTypeface() { 
     if (getId() == R.id.text2 || getId() == R.id.text4) {           
      super.setTypeface(bf);  
     } 
     else if (getId() == R.id.text3 || getId() == R.id.text5) { 
      super.setTypeface(rn); 
     } 
    } 

} 

EDIT: Обновлен код. Автоматическое обновление шрифта.

+0

он не меняет шрифт – androidqq6

+0

Обновлен код для установки шрифта автоматически –

+0

такой же не меняющийся шрифт – androidqq6

Смежные вопросы