android自定义字体没有显示

时间:2014-07-07 20:32:54

标签: android fonts

我遇到一个问题,当我为视图指定自定义字体时没有显示任何内容。

TextView statsView1, statsView2, statsView;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.fragment_main);



    statsView=(TextView) findViewById(R.id.statsView);
    statsView1 = (TextView) findViewById(R.id.stats1);
    statsView2 = (TextView) findViewById(R.id.stats2);
    Typeface font=Typeface.createFromAsset(getAssets(), "Pretendo.ttf");
    statsView.setTypeface(font);
    statsView2.setTypeface(font);
    InitialSetupUI();
    Calculate();
}

所以我首先在on create方法中指定文本视图和字体。

public void Calculate() {

    calculate = (Button) findViewById(R.id.calculate);

    calculate.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {


            TotalStats stats1 = new TotalStats(player, car, wheel, glide);
            TotalStats stats2 = new TotalStats(player2, car2, wheel2,
                    glide2);
            statsView1.setText(String.valueOf(stats1.speed) + "\n"
                    + String.valueOf(stats1.speedWater) + "\n"
                    + String.valueOf(stats1.speedAir) + "\n"
                    + String.valueOf(stats1.speedGround) + "\n"
                    + String.valueOf(stats1.accel) + "\n"
                    + String.valueOf(stats1.weight) + "\n"
                    + String.valueOf(stats1.hand) + "\n"
                    + String.valueOf(stats1.handWater) + "\n"
                    + String.valueOf(stats1.handAir) + "\n"
                    + String.valueOf(stats1.handGround) + "\n"
                    + String.valueOf(stats1.trac) + "\n"
                    + String.valueOf(stats1.mt) + "\n");
            statsView2.setText(String.valueOf(stats2.speed) + "\n"
                    + String.valueOf(stats2.speedWater) + "\n"
                    + String.valueOf(stats2.speedAir) + "\n"
                    + String.valueOf(stats2.speedGround) + "\n"
                    + String.valueOf(stats2.accel) + "\n"
                    + String.valueOf(stats2.weight) + "\n"
                    + String.valueOf(stats2.hand) + "\n"
                    + String.valueOf(stats2.handWater) + "\n"
                    + String.valueOf(stats2.handAir) + "\n"
                    + String.valueOf(stats2.handGround) + "\n"
                    + String.valueOf(stats2.trac) + "\n"
                    + String.valueOf(stats2.mt) + "\n");

        }

    });

}

忽略所有新行.. 然后我尝试将文本输出到视图但没有显示任何内容。值得注意的是,statView textview只包含xml中的文本,并且确实显示出来。

 <TextView
        android:id="@+id/statsView"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="3"
        android:gravity="center"
        android:text="Speed\nWater Speed\nAir Speed\nGround Speed\nAcceleration\nWeight\nHandling\nWater Handling\nAir Handling\nGround Handling\nTraction\nMini Turbo"
        android:textSize="14sp" />

我认为问题是在功能之间传递文本视图?任何帮助表示赞赏。

更新: 我将statView2 setText值更改为

    statsView2.setText("test");

它成功打印出来&#34; test&#34;在字体中。所以这意味着问题出在这些方面

statsView1.setText(String.valueOf(stats1.speed) + "\n"
                + String.valueOf(stats1.speedWater) + "\n"
                + String.valueOf(stats1.speedAir) + "\n"
                + String.valueOf(stats1.speedGround) + "\n"
                + String.valueOf(stats1.accel) + "\n"
                + String.valueOf(stats1.weight) + "\n"
                + String.valueOf(stats1.hand) + "\n"
                + String.valueOf(stats1.handWater) + "\n"
                + String.valueOf(stats1.handAir) + "\n"
                + String.valueOf(stats1.handGround) + "\n"
                + String.valueOf(stats1.trac) + "\n"
                + String.valueOf(stats1.mt) + "\n");

但我不知道什么可能影响它不输出,因为它输出默认字体。

1 个答案:

答案 0 :(得分:0)

您可能必须将getAssets()更改为getContext()。getAssets()以获取以下行中的.ttf文件: -
字体font = Typeface.createFromAsset(getAssets(),&#34; Pretendo.ttf&#34;);