我为抽屉创建了一个自定义类。除了一点之外,它运作良好。
自定义字体无效。
当我在“onCreate()”中自定义字体时,它可以正常工作。当抽屉打开时,ActionBar标题会改变。我设置了一个自定义字体,如下所示(与onCreate中的代码相同),但它不起作用。
在ActionBarActivity中,它被称为:
Drawer drawer = new Drawer(this);
drawer.setUpDrawer();
有我的Drawer.class。
public class Drawer {
/** Drawer attr **/
private ListView mDrawerList;
private DrawerLayout mDrawerLayout;
private ActionBarDrawerToggle mDrawerToggle;
private CharSequence mDrawerTitle;
private CharSequence mTitle;
private int positionDrawer;
private Activity a;
public Drawer(Activity a) {
this.a = a;
}
/**
* Set Up the drawer with the array list of items, add animation listener etc...
*/
public void setUpDrawer() {
//Get DrawerLayout
mDrawerLayout = (DrawerLayout) a.findViewById(R.id.drawer_layout);
//Get Listview into drawer
mDrawerList = (ListView) a.findViewById(R.id.left_drawer);
// set a custom shadow that overlays the main content when the drawer opens
mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START);
// set up the drawer's list view with items and click listener
mDrawerList.setAdapter(new DrawerAdapter(a, new DrawerList().getDrawerList()));
mDrawerList.setOnItemClickListener(new DrawerItemClickListener(a));
// enable ActionBar app icon to behave as action to toggle nav drawer
((ActionBarActivity) a).getSupportActionBar().setDisplayHomeAsUpEnabled(true);
// ActionBarDrawerToggle ties together the proper interactions
// between the sliding drawer and the action bar app icon
mDrawerToggle = new ActionBarDrawerToggle(
a, /* host Activity */
mDrawerLayout, /* DrawerLayout object */
R.drawable.ic_drawer, /* nav drawer image to replace 'Up' caret */
R.string.drawer_open, /* "open drawer" description for accessibility */
R.string.drawer_close /* "close drawer" description for accessibility */
) {
public void onDrawerClosed(View view) {
Log.d("TAG", "drawer close");
Typeface tf = Typeface.createFromAsset(a.getAssets(), "Anothershabby_trial.ttf");
SpannableStringBuilder SS = new SpannableStringBuilder(a.getTitle());
SS.setSpan (new CustomTypefaceSpan(tf), 0, SS.length(), Spanned.SPAN_EXCLUSIVE_INCLUSIVE);
((ActionBarActivity) a).getSupportActionBar().setTitle(" "+SS);
a.invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
}
public void onDrawerOpened(View drawerView) {
Log.d("TAG", "drawer open");
Typeface tf = Typeface.createFromAsset(a.getAssets(), "Anothershabby_trial.ttf");
SpannableStringBuilder SS = new SpannableStringBuilder(a.getResources().getString(R.string.drawer_open));
SS.setSpan (new CustomTypefaceSpan(tf), 0, SS.length(), Spanned.SPAN_EXCLUSIVE_INCLUSIVE);
((ActionBarActivity) a).getSupportActionBar().setTitle(" "+SS);
a.invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
}
};
mDrawerLayout.setDrawerListener(mDrawerToggle);
}
}
求助于你