更改Font时出现语法错误

时间:2014-08-09 21:50:42

标签: string android-activity syntax fonts assets

我正在尝试更改textView的字体(在我的情况下是custom_font) 但如果在这一行给出2错误:

txt.setTypeface(font);

在(点)它说:
令牌上的语法错误,错位的构造

在(字体)它说:

令牌“font”上的语法错误,此令牌后面的VariableDeclaratorId

我不知道如何修复它,如果有人能帮助我,我会很高兴。)

提前致谢。

MainActivity:

package com.test.testforStack;

import android.support.v7.app.ActionBarActivity;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;


public class MainActivity extends ActionBarActivity {

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

TextView txt = (TextView) findViewById(R.id.custom_font);
Typeface font = Typeface.createFromAsset(getAssets(), "fonts/hallo.ttf");
txt.setTypeface(font);

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
 }

1 个答案:

答案 0 :(得分:0)

尝试将这三行移动到onCreate方法中:

TextView txt = (TextView) findViewById(R.id.custom_font);
Typeface font = Typeface.createFromAsset(getAssets(), "fonts/hallo.ttf");
txt.setTypeface(font);