更改ActionBar的字体?

时间:2014-12-17 16:57:12

标签: android android-fragments

我正在寻找改变ActionBar字体的解决方案。我有一个扩展ActionBarActivity的CustomDrawerLayout,当它打开Fragment时,我希望在ActionBar上使用自定义字体显示Fragment的标题。

我尝试过使用TypedFace:Typeface tf = Typeface.createFromAsset(getAssets(),"fonts/CarterOne-1.ttf");,但我无法正常工作

我该怎么做?

我正在尝试这个。

XML

<android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/dl"
     >

   <FrameLayout 
       android:id="@+id/fl"
       android:layout_width="match_parent"
       android:layout_height="match_parent"       
       >       
   </FrameLayout>

   <ListView
       android:id="@+id/lv"
       android:layout_width="250dp"
       android:layout_height="match_parent"
       android:choiceMode="singleChoice"
        android:divider="#e9ba68"
        android:paddingLeft="5dp"
        android:paddingRight="5dp"
        android:dividerHeight="1dp"
        android:background="#ac453c"
        android:layout_gravity="start"
       >

   </ListView>

</android.support.v4.widget.DrawerLayout>

CustomDrawerLayout

public class CustomDrawerLayout extends ActionBarActivity implements OnItemClickListener{
    private ActionBar ab;
    private DrawerLayout dl;
    private ListView lv;
    private ActionBarDrawerToggle tg;

    private List<ItensListView> fragments;
    private CharSequence tl; //titulo principal
    private CharSequence tlf; //titulo fragment 

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_custom_drawerlayout);
        getSupportActionBar().setBackgroundDrawable(new ColorDrawable(getResources().getColor(R.color.action_bar)));
        init();

        if(savedInstanceState == null){
            selectedItem(0);
        }       
    }

    private void init(){
        //actionbar
        onConfigActionBar();
        //listview
        configItensListView();
        lv = (ListView)findViewById(R.id.lv);               
        lv.setAdapter(new DrawerLayoutListViewAdapter(this, fragments));
        lv.setOnItemClickListener(this);        
        //drawerlayout
        dl = (DrawerLayout)findViewById(R.id.dl);
        //actionbardrawertoggle
        tg = new ActionBarDrawerToggle(this, dl, R.drawable.btmenu, R.string.nomeActionBar){
            public void onDrawerClosed(View view) {
                ab.setTitle(tl);                
                supportInvalidateOptionsMenu();
            }

            public void onDrawerOpened(View view) {
                ab.setTitle(tlf);
                supportInvalidateOptionsMenu();
            }
        };      
        dl.setDrawerListener(tg);

        tl = tlf = getTitle();      
    }

    /** ativa actionbar e botao home na action bar */
    private void onConfigActionBar(){
        ab = getSupportActionBar();
        ab.setDisplayHomeAsUpEnabled(true);
        ab.setHomeButtonEnabled(true);
    }




    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        tg.onConfigurationChanged(newConfig);
    }

    /** necessario */
    @Override
    protected void onPostCreate(Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);
        tg.syncState();
    }

    /** necessario */
     @Override
     public boolean onOptionsItemSelected(MenuItem item) {
         FragmentTransaction ft;
         Fragment frag;

         if(item.getItemId() == R.id.action_chat){
             frag = new HelloBubblesActivity();
             ft = getSupportFragmentManager().beginTransaction();
             ft.replace(R.id.fl, frag, "HelloBubblesActivity");
             ft.addToBackStack("back");
             ft.commit();
         }

         if (tg.onOptionsItemSelected(item)) {
                return true;
         }
         return super.onOptionsItemSelected(item);
     }


     /** necessario */
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.custom_drawer_layout, menu);

        return true;
    }

    /** necessario */
    @Override
    public boolean onPrepareOptionsMenu(Menu menu) {
        boolean status = dl.isDrawerOpen(lv);
        menu.findItem(R.id.action_settings).setVisible(!status);
        return super.onPrepareOptionsMenu(menu);
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position,long id) {
        selectedItem(position);     
    }

    /** seleciona o fragment q sera usado */
    private void selectedItem(int position){
        FragmentTransaction ft;
        Fragment frag;
        switch(position){
            case 0:
                //frag = new InicioFrag();
                frag = new InicioFrag();
                ft = getSupportFragmentManager().beginTransaction();
                ft.replace(R.id.fl, frag);
                ft.addToBackStack("back");
                ft.commit();
                break;
            case 1:
                frag = new ApresentacaoFrag();
                ft = getSupportFragmentManager().beginTransaction();
                ft.replace(R.id.fl, frag);
                ft.addToBackStack("back");
                ft.commit();
                break;
            case 3:
                frag = new PerfilFrag();
                ft = getSupportFragmentManager().beginTransaction();
                ft.replace(R.id.fl, frag);
                ft.addToBackStack("back");
                ft.commit();
                break;

        }

        lv.setItemChecked(position, true);
        setCustomTitle(fragments.get(position).getTexto());
        dl.closeDrawer(lv);
    }

    /** change title of ActionBar */
    private void setCustomTitle(String title){
        Typeface tf = Typeface.createFromAsset(getAssets(),"font/CarterOne-1.ttf");
        ab.setTitle(title);
        tl = title;
    }

0 个答案:

没有答案