Setting a text font in android tab widget not working?
I'm trying to change the font of text in one of the activities of a tab Layout. I'm using a custom font. But the font isn't working. for one of my classes, when i switch to that tab i get an error from the eclipse debugger but there seems to be absolutely nothing wrong with my code. This is the code that is failing
public class YearThreeActivity extends Activity{
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
this.setContentView(R.layout.yearthree);
/*without the next three lines the tab view works fine */
TextView tv=(TextView)findViewById(R.id.yearthree_view);//<-- debugger says this assignment is null but which shouldn't be the case as the R class has that in the id subclass.
Typeface font = Typeface.createFromAsset(this.getAssets(), "fonts/Century_Gothic.ttf");
tv.setTypeface(font);
}
}
However this activity has pretty much the exact same thing and when I switch to this tab it works
public class StatsActivity extends Activity{
public void onCreate(Bundle savedInstanceS开发者_Python百科tate){
super.onCreate(savedInstanceState);
this.setContentView(R.layout.stats);
TextView tv=(TextView)findViewById(R.id.stats_view);
Typeface font = Typeface.createFromAsset(this.getAssets(), "fonts/Century_Gothic.ttf");
tv.setTypeface(font);
}
}
I've been at this for hours checking. The xml manifest files, the layout files, everything is the same with the both of them however the first one doesn't work... I'm here as a last resort... :<
When I use tabs, I normally just hide the tabwidget tag by setting android visibility as gone.
And add buttons to act as the tab buttons like
<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/tabhost" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<FrameLayout android:id="@android:id/tabcontent"
android:layout_width="fill_parent" android:layout_height="0dip"
android:layout_weight="1.0"/>
<FrameLayout android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TabWidget android:id="@android:id/tabs" android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:visibility="gone"/>
<LinearLayout android:layout_width="fill_parent"
android:layout_height="64dip">
<Button android:layout_height="fill_parent" android:layout_width="0dip"
android:layout_weight="1.0"
android:background="@drawable/ic_tab_artists"
android:id="@+id/artist_id" android:onClick="tabHandler"/>
<Button android:layout_height="fill_parent" android:layout_width="0dip"
android:layout_weight="1.0"
android:background="@drawable/ic_tab_artists"
android:id="@+id/album_id" android:onClick="tabHandler"/>
<Button android:layout_height="fill_parent" android:layout_width="0dip"
android:layout_weight="1.0"
android:background="@drawable/ic_tab_artists"
android:id="@+id/song_id" android:onClick="tabHandler"/>
</LinearLayout>
</FrameLayout>
</LinearLayout>
</TabHost>
and I add a button click handler
public void tabHandler(View target){
artistButton.setSelected(false);
albumButton.setSelected(false);
songButton.setSelected(false);
if(target.getId() == R.id.artist_id){
tabHost.setCurrentTab(0);
artistButton.setSelected(true);
} else if(target.getId() == R.id.album_id){
tabHost.setCurrentTab(1);
albumButton.setSelected(true);
} else if(target.getId() == R.id.song_id){
tabHost.setCurrentTab(2);
songButton.setSelected(true);
}
}
When I use this method, it gives me more freedom to style the tab buttons.
I fixed it. It had to do with the way I gave textViews in my application id's. They coincided
精彩评论