2015-05-05 33 views
4

我想更改按鈕的字體系列。我試圖改變它嘗試此代碼:android工作室中的按鈕更改字體系列

Button txt = (Button)findViewById(R.id.button1); 
Typeface font = Typeface.createFromAsset(getAssets(), "sfont.ttf"); 
txt.setTypeface(font); 

但我收到一個錯誤,如果我忽略它的應用程序每次崩潰。

這是佈局XML代碼:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_height="fill_parent" 
android:layout_width="fill_parent" 
android:orientation="vertical" 
> 

<TextView 

    android:text="@string/enter" 
    android:textColor="#000" 
    android:textSize="16dp" 
    android:textAlignment="center" 
    android:id="@+id/**button1**"text1 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_gravity="center" 
    android:padding="20dp" /> 

<Button 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:id="@+id/**text1**" button1 
    android:padding="30dp" 
    android:layout_gravity="center" 
    android:text="@string/entenbut" 
    /> 

問題解決了有一個在XML代碼中的問題 謝謝大家:)

+1

你介意與我們分享異常的堆棧跟蹤嗎? – Blackbelt

+0

對不起,但我怎麼能得到異常的堆棧跟蹤?這是我的第一個應用程序:) –

+0

檢查logcat的 – Blackbelt

回答

4

做一個文件夾命名爲「資產「在您的文件夾」src「(如果您使用Android Studio)並使用:

Typeface tfFutura = Typeface.createFromAsset(getAssets(), "sfont.ttf"); 

觀察:看看你的文件是「TTF」還是「ttf」。我遇到了問題。

+0

沒有它的工作,我可以用相同的代碼編輯TextView的字體系列,但不是按鈕 –

+0

請顯示堆棧跟蹤日誌。發生錯誤時,您可以讓他進入logcat。 –

+0

這是它: https://app.box.com/s/zst4c76cwzwiteqq5tdtvcvy7sgleeyc –

1

在android studio中沒有目錄「src」,但有「res」。 創建資產目錄,首先右鍵單擊「應用程序」,然後: 新建 - >文件夾 - >資產文件夾。之後,將字體複製到此目錄。 click here for more info

+1

謝謝!這篇文章正是我所期待的。 –