2014-05-11 77 views
0

我试图将textview文本设置为自定义字体。Android - 将自定义字体应用到textview中不起作用

为了做到这一点,我已经做了接下来的步骤 -

1)布局XML正在寻找像这样:

  <TextView 
       android:id="@+id/textView_window_title" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_gravity="center_vertical" 
       android:text="Profile" 
       android:textColor="#505A62" /> 

2)我已经创建了内部的一个文件夹fontsassets文件夹并放置3个不同的字体文件。他们三个都是.ttf文件类型。另外每个字体文件类型的名字都写在小字母,就像这样 - cr.ttf

3)在片段类我用下面的代码 -

@Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
     Bundle savedInstanceState) { 

      View v = inflater.inflate(R.layout.gd_menu_layout, container, false); 

      Typeface tf = Typeface.createFromAsset(getActivity().getAssets(), 
       "fonts/cr.ttf"); 
      titleWindow = (TextView) v.findViewById(R.id.textView_window_title); 
      titleWindow.setTypeface(tf); 


      return v; 

     } 

我已经试过了3个不同的字体文件,但他们都没有改变textview字体。

任何想法为什么此代码不起作用?

感谢任何形式的帮助

+0

你可能看不到此代码写入IDE中的任何改变,但是当你构建应用程序,你会看到字体的变化,当你在模拟器中运行它。 –

+0

嗯,我试图在真实的设备上运行它 - 看不到任何改变,所以这就是为什么我说这个代码不适合我 – 4this

+0

我已经将字体文件从字体文件夹移动到资产管理器,也尝试过使用字符串的东西 - 仍然不工作 – 4this

回答

1
String fontPath = "fonts/cr.ttf"; 
Typeface tf = Typeface.createFromAsset(this.getAssets(), fontPath); 
titleWindow.setTypeface(tf); 

试试这个

+0

非常感谢您的帮助,但这不适合我。 – 4this

+0

ckeck你的文件夹名称和文件名是否与你提到的相同 – Meghna

+0

是的他们是,我已经检查了两次,然后我已经发布这个questoin在这里 – 4this

0

设置你的字体字体在您使用的所有视图上,只需尝试我的代码...

private EditText etComment2; 
private TextView tvQuestionText1; 
private TextView tvQuestionText2; 
private Button btn1; 


    in you onCreate() 

    etComment3 = (EditText)findViewById(R.id.etComment3); 
    tvQuestionText1 = (TextView)findViewById(R.id.tvQuestionText1); 
    tvQuestionText2 = (TextView)findViewById(R.id.tvQuestionText2); 
    btn1 = (Button)findViewById(R.id.btn1); 

创建的所有意见阵列这里 -

View allViews[] = 
     { 
      etComment2, 
      tvQuestionText1, 
      tvQuestionText2, 
      btn1 
     }; 

通过此视图对象这里 - AppConstant是我们班来管理所有的静态方法

AppConstant.setTypeFace(allViews); 

现在这里是我们不变的类方法TypeFace

public static void setTypeFace(View []views) 
{ 

    for(int i=0 ; i<views.length; i++) 
    { 
     Typeface tf = Typeface.createFromAsset(views[i].getContext().getAssets(), "fonts/SourceSansPro-Semibold.ttf"); 
     if(views[i] instanceof TextView) 
     { 
      ((TextView)views[i]).setTypeface(tf); 
     } 
     else if(views[i] instanceof Button) 
     { 
      ((Button)views[i]).setTypeface(tf); 
     } 
     else if(views[i] instanceof EditText) 
     { 
      ((EditText)views[i]).setTypeface(tf); 
     } 
    } 
0
// Declare styleable in attrs.xml 
/res/values/attrs.xml 

<?xml version="1.0" encoding="utf-8"?> 
<resources> 
<declare-styleable name="MyTextView"> 
<attr name="fontName" format="string" /> 
    </declare-styleable> 
</resources> 

Create a layout in layout folder 

    /res/layout/activity_main.xml 
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     xmlns:customfontdemo="http://schemas.android.com/apk/res-auto" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:orientation="vertical" 
     android:gravity="center"> 

     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:textSize="24sp" 
      android:padding="12dp" 
      android:text="Standard Android Font" /> 

     <com.authorwjf.customfontdemo.MyTextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:textSize="32sp" 
      android:padding="12dp" 
      customfontdemo:fontName="pipe_dream.ttf" 
      android:text="Custom Android Font" /> 

    </LinearLayout> 

//现在创建自定义的TextView

/src/MyTextView.java

package com.deepak.utilsview; 

import android.content.Context; 
import android.content.res.TypedArray; 
import android.graphics.Typeface; 
import android.util.AttributeSet; 
import android.widget.TextView; 

public class MyTextView extends TextView { 

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

    public MyTextView(Context context, AttributeSet attrs) { 
     super(context, attrs); 
     init(attrs); 

    } 

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

    private void init(AttributeSet attrs) { 
     if (attrs!=null) { 
      TypedArray a = getContext().obtainStyledAttributes(attrs, R.styleable.MyTextView); 
      String fontName = a.getString(R.styleable.MyTextView_fontName); 
      if (fontName!=null) { 
       Typeface myTypeface = Typeface.createFromAsset(getContext().getAssets(), "fonts/"+fontName); 
       setTypeface(myTypeface); 
      } 
      a.recycle(); 
     } 
    } 

} 

//现在看到您的活动的字体运行活动

/src/MainActivity.java 包后com.authorwjf.customfontdemo;

import android.os.Bundle; 
import android.app.Activity; 

public class MainActivity extends Activity { 

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

} 
相关问题