Java Can';t在按钮上设置字体-不是NullPointerException,但字体在程序中永远不会改变
我已经尝试了一段时间,到处寻找,想弄明白这一点,但没有运气。 简而言之,我的问题是:我无法通过编程设置按钮的字体。我可以用findViewById很好地获得它,然后我创建我的字体,并设置它,没有任何东西会破坏一切正常。除了变化从未出现在节目中。它仍然是默认的字体 这是我的密码Java Can';t在按钮上设置字体-不是NullPointerException,但字体在程序中永远不会改变,java,android,user-interface,fonts,Java,Android,User Interface,Fonts,我已经尝试了一段时间,到处寻找,想弄明白这一点,但没有运气。 简而言之,我的问题是:我无法通过编程设置按钮的字体。我可以用findViewById很好地获得它,然后我创建我的字体,并设置它,没有任何东西会破坏一切正常。除了变化从未出现在节目中。它仍然是默认的字体 这是我的密码 public class MainActivity extends FragmentActivity { Button lcb; Typeface resoLite; private SplashFragment
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);
}
我的活动和主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>
我已经用Logs和Typeface.equals()测试了setTypeface,以检查它是否设置为我所期望的,并且看起来是这样的。它只是永远不会改变屏幕上的实际按钮。我觉得我明显遗漏了什么。有什么想法吗
编辑:为了清楚起见,我正在尝试将字体设置为项目中我的资产/字体/文件夹中的自定义字体,因此在xml文件中设置字体将不起作用。首先将字体放置在文件夹资产/字体/your_font.ttf 现在在Activity/Fragment类中添加以下代码行:
Typeface myTypeface = Typeface.createFromAsset(getAssets(), "fonts/your_font.ttf");
Button custom_btn = (Button) findViewById(R.id.btn);
custom_btn.setTypeface(myTypeface);
custom_btn.setOnClickListener(this);