2017-09-16 38 views
0

我創建了下面的按鈕叫的Android編程按鈕字體不改變

Button loginButton = (Button) findViewById(R.id.login_button); 
Typeface tf = Typeface.createFromAsset(getAssets(), "fonts/Lato-Regular.ttf"); 
loginButton.setTypeface(tf); 

我確定字體是在資產/字體。確切的文件名是:/ Users/myusername/Documents/GitHub/appname /app/src/main/assets/fonts/Lato-BoldItalic.ttf。

但是,按鈕上的字體仍然不會改變。這裏可能是什麼問題?

編輯:擴展的代碼

protected void onCreate(Bundle savedInstanceState) { 
    Parse.initialize(new Parse.Configuration.Builder(this) 
      .applicationId("###") 
      .server("https://parseapi.back4app.com/") 
      .clientKey("###") 
      .enableLocalDataStore() 
      .build() 
    ); 

    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_login); 

    Button loginButton = (Button) findViewById(R.id.login_button); 
    Typeface tf = Typeface.createFromAsset(getAssets(), "Lato-Regular.ttf"); 
    loginButton.setTypeface(tf); 

more stuff 

回答

0

把你的字體文件資產文件夾,然後嘗試訪問這樣

創建您的資產文件夾下面的樹結構

--\app 
-----\src 
----------\main 
--------------\assets 

然後把你的資產文件夾內的字體文件

Button loginButton = (Button) findViewById(R.id.login_button); 
Typeface tf = Typeface.createFromAsset(getAssets(), "Lato-Regular.ttf"); 
loginButton.setTypeface(tf); 

希望它會幫助你..

+0

我試過這個,但它仍然不起作用。 :/我不確定可能存在什麼其他問題。 – chickenwings

+0

它仍然不工作的原因:( – chickenwings

+0

看到我的更新後 – Raja

0

字體名稱代碼(Lato-Regular.ttf)不同於然後在你的文件系統(Lato-BoldItalic.ttf)的名稱。

此外,Android無法處理所有可能的字體。在這些情況下,它悄然失敗並默認爲常規字體。您可能需要嘗試其他字體。

+0

我有資產文件夾中的兩種字體,我只是改變它爲BoldItalic,所以改變會更清楚,如果我的眼睛只是壞。嘗試不同的字體,看看是否是這個問題。 – chickenwings

0

您已經從文件夾中指出,當您設置fonts/Lato-Regular.ttf時,它是Lato-BoldItalic.ttf。編輯你的代碼!設置您添加到項目中的所需字體。