设置文本视图android的字体(来自字体文件)

时间:2012-06-16 13:45:25

标签: android android-layout

我有一个字体文件love.ttf(5mb)。我把它放到资产文件夹中。 这是我的代码

 @Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    TextView tv=(TextView)findViewById(R.id.textView1);
    Typeface tf=Typeface.createFromAsset(getAssets(),"love.ttf");;
    tv.setTypeface(tf,Typeface.NORMAL);
    tv.setTextSize(20);
}  

main.xml文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#4B67A1"
    android:orientation="vertical" >    
    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="おはようございます。" />    
</LinearLayout>

当我运行它时,我看不到文字! 如何加载大字体? 请帮我! 感谢

2 个答案:

答案 0 :(得分:2)

因此,首先会有两个可能的原因导致它不起作用。

  1. 检查您的字体是否支持日文字符。
  2. 您的字体文件路径可能不正确。

    TextView tv = (TextView) findViewById(R.id.textView1);
    Typeface tf=Typeface.createFromAsset(getAssets(),"fonts/love.ttf");
    
  3. 您应该在名为assets的{​​{1}}文件夹中有下一个子文件夹。然后还要检查你是否有正确的字体后缀。

答案 1 :(得分:0)

我建议你不要直接在Asset文件夹中保存字体,而是在它下面创建字体文件夹,然后保存love.ttf字体。

然后你可以执行如下魔术。

  TextView tv=(TextView)findViewById(R.id.custom); 
  Typeface face=Typeface.createFromAsset(getAssets(), "fonts/love.ttf"); 
  tv.setTypeface(face);