0
我一直在嘗試一段時間,並且在很多地方搜索瞭解這個,但沒有運氣。 我的問題是,簡而言之:我無法以編程方式設置我的按鈕的字體。我可以用findViewById得到它,然後創建我的字體,並設置它,沒有任何東西打破一切似乎正常。除了變化從未出現在程序中。它仍然是默認字體。不能在一個按鈕上設置字體 - 不是NullPointerException,但字體在程序中永遠不會改變
這是我的代碼。
public class MainActivity extends FragmentActivity {
Button lcb;
Typeface resoLite;
private SplashFragment splashFragment;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
// Add the fragment on initial activity setup
splashFragment = new SplashFragment();
getSupportFragmentManager()
.beginTransaction()
.add(android.R.id.content, splashFragment)
.commit();
} else {
// Or set the fragment from restored state info
splashFragment = (SplashFragment) getSupportFragmentManager()
.findFragmentById(android.R.id.content);
}
SpannableString s = new SpannableString("resocializer");
s.setSpan(new TypefaceSpan(this, "titillium-bold"), 0, s.length(),
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
// Update the action bar title with the TypefaceSpan instance
ActionBar actionBar = getActionBar();
//actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_TITLE);
actionBar.setTitle(s);
//Here's the important stuff. I get the button fine. I create the typeface
//fine. I set the typeface fine. but none of this appears to have any effect
//in the actual program when it's running.
lcb = (Button) findViewById(R.id.lcb);
resoLite = Typeface.createFromAsset(getAssets(), "fonts/titillium-bold.otf");
lcb.setTypeface(resoLite);
}
而且我activity_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:orientation="vertical" >
<com.facebook.widget.LoginButton
android:id="@+id/authButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="30dp"
/>
<Button
android:id="@+id/lcb"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/lcbText"
android:layout_gravity="center_horizontal"
android:layout_marginTop="60dp"
android:background="#C4A649"
android:textColor="#FFFFFF"
android:onClick="logConversation"/>
</LinearLayout>
我已經測試使用日誌和Typeface.equals()的setTypeface來檢查它是否設置爲我所期望它,它似乎是如此。它從來沒有改變在屏幕上的實際按鈕。我覺得我有一些明顯的缺失。有任何想法嗎?
編輯:爲了清楚起見,我嘗試在項目中的資產/字體/文件夾中將字體設置爲自定義字體,因此將其設置在xml文件中將不起作用。