Android:从导航抽屉更改ActionBar中的标题

Android:从导航抽屉更改ActionBar中的标题,android,android-actionbar,title,navigation-drawer,Android,Android Actionbar,Title,Navigation Drawer,我在BaseActivity中创建了一个带有导航抽屉的应用程序。除了在操作栏中更改标题外,一切正常。标题会更改一秒钟,但打开新活动时会显示原始标题 可能是什么错误?谢谢 基本活动 protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); PerfilAdapter.iniciarBaseDatos(this); perfilObj = PerfilA

我在
BaseActivity
中创建了一个带有
导航抽屉的应用程序。除了在
操作栏中更改标题外,一切正常。标题会更改一秒钟,但打开新活动时会显示原始标题

可能是什么错误?谢谢

基本活动

protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    PerfilAdapter.iniciarBaseDatos(this);
    perfilObj = PerfilAdapter.selectPerfil(1);

    requestWindowFeature(Window.FEATURE_ACTION_BAR);

    cargarActionBar();
    cargarDrawerLayout(savedInstanceState);
}

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

    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {

    if (mDrawerToggle.onOptionsItemSelected(item)) {
        return true;
    }

    switch (item.getItemId()) {

    case R.id.menuOpcSonidos:

                   ...
                   return true;

    case R.id.menuOpcCambiarColor:
        ...
        return true;

    default:
        return super.onOptionsItemSelected(item);
    }
}

@Override
public boolean onPrepareOptionsMenu(Menu menu) {

    menu.findItem(R.id.menuOpcSonidos)
        .setTitle(getResources().getString(R.string.sonidoOnOff) + " " + perfilObj.getSonidos());

    return super.onPrepareOptionsMenu(menu);
}

private void cargarActionBar() {

    ActionBar actionBar = getActionBar();
    int[] colores2 = Modulo.cargarColoresDrawerlayout(perfilObj.getColor());
    actionBar.setBackgroundDrawable(new GradientDrawable(Orientation.BOTTOM_TOP, colores2));

    getActionBar().setDisplayHomeAsUpEnabled(true);
    getActionBar().setHomeButtonEnabled(true);

    int titleId = Resources.getSystem().getIdentifier("action_bar_title", "id", "android");
    TextView textoTitulo = (TextView)findViewById(titleId);
    textoTitulo.setTextColor(getResources().getColor(R.color.blanco));
    textoTitulo.setTypeface(null, Typeface.BOLD);
    textoTitulo.setTextSize(19);
    textoTitulo.setShadowLayer(5, 0, 0, getResources().getColor(R.color.negro));        
}

private void cargarDrawerLayout(Bundle b) {

    mTitle = mDrawerTitle = getTitle();

    textosMenuLateral = getResources().getStringArray(R.array.nav_drawer_items);

    iconosMenuLateral1 = getResources()
            .obtainTypedArray(R.array.iconos_menu_lateral1);

    iconosMenuLateral2 = getResources()
            .obtainTypedArray(R.array.iconos_menu_lateral2);

    mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
    mDrawerList = (ListView) findViewById(R.id.list_slidermenuMain);

    int[] colores = {0, 0xFFFFFFFF, 0};
    mDrawerList.setDivider(new GradientDrawable(Orientation.RIGHT_LEFT, colores));
    mDrawerList.setDividerHeight(4);

    navDrawerItems1 = new ArrayList<DrawerItem>();

    navDrawerItems1.add(new DrawerItem(textosMenuLateral[0], iconosMenuLateral1.getResourceId(0, -1)));
    navDrawerItems1.add(new DrawerItem(textosMenuLateral[1], iconosMenuLateral1.getResourceId(1, -1)));
    navDrawerItems1.add(new DrawerItem(textosMenuLateral[2], iconosMenuLateral1.getResourceId(2, -1)));
    navDrawerItems1.add(new DrawerItem(textosMenuLateral[3], iconosMenuLateral1.getResourceId(3, -1)));
    navDrawerItems1.add(new DrawerItem(textosMenuLateral[4], iconosMenuLateral1.getResourceId(4, -1)));

    navDrawerItems2 = new ArrayList<DrawerItem>();

    navDrawerItems2.add(new DrawerItem(textosMenuLateral[0], iconosMenuLateral2.getResourceId(0, -1)));
    navDrawerItems2.add(new DrawerItem(textosMenuLateral[1], iconosMenuLateral2.getResourceId(1, -1)));
    navDrawerItems2.add(new DrawerItem(textosMenuLateral[2], iconosMenuLateral2.getResourceId(2, -1)));
    navDrawerItems2.add(new DrawerItem(textosMenuLateral[3], iconosMenuLateral2.getResourceId(3, -1)));
    navDrawerItems2.add(new DrawerItem(textosMenuLateral[4], iconosMenuLateral2.getResourceId(4, -1)));

    iconosMenuLateral1.recycle();
    iconosMenuLateral2.recycle();

    mDrawerList.setOnItemClickListener(new SlideMenuClickListener());

    adapter = new DrawerListAdapter(getApplicationContext(),
            navDrawerItems1,
            navDrawerItems2,
            perfilObj.getColor(),
            pos);
    mDrawerList.setAdapter(adapter);

    mDrawerToggle = new ActionBarDrawerToggle(
            this, 
            mDrawerLayout,
            R.drawable.icono_drawer, 
            R.string.app_name,
            R.string.app_name
    ) {
        public void onDrawerClosed(View view) {

            getActionBar().setTitle(mTitle);
            invalidateOptionsMenu();
        } 

        public void onDrawerOpened(View drawerView) {

            getActionBar().setTitle(mDrawerTitle);

            invalidateOptionsMenu();
        }
    };
    mDrawerLayout.setDrawerListener(mDrawerToggle);

    if (b == null) {

        opcionesPanelLateral(0);
    }

}

private class SlideMenuClickListener implements ListView.OnItemClickListener {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position,
            long id) {

        TextView textView = (TextView) view.findViewById(R.id.title);
        textView.setTypeface(null, Typeface.BOLD);    

        opcionesPanelLateral(position);
    }
}

private void opcionesPanelLateral(int position) {

    Intent i;

    switch (position) {
        case 0:
            pos = 0;
            break;

        case 1:
            i = new Intent(this, ActivitySecond.class);
            mDrawerLayout.closeDrawer(mDrawerList);
            startActivity(i);
            pos = 1;
            break;

        case 2:
            i = new Intent(this, ActivityThird.class);
            mDrawerLayout.closeDrawer(mDrawerList);
            startActivity(i);
            pos = 2;
            break;

        case 3:
            i = new Intent(this, ActivityFourth.class);
            mDrawerLayout.closeDrawer(mDrawerList);
            startActivity(i);
            pos = 3;
            break;

        case 4:

            break;

        default:
            break;
    }

    mDrawerList.setItemChecked(pos, true);
    mDrawerList.setSelection(pos);

    setTitle(textosMenuLateral[pos]);


    mDrawerLayout.closeDrawer(mDrawerList);
}


@Override
public void setTitle(CharSequence title) {
    mTitle = title;
    getActionBar().setTitle(mTitle);
}

@Override
protected void onPostCreate(Bundle savedInstanceState) {

    super.onPostCreate(savedInstanceState);

    cargarActionBar();
    cargarDrawerLayout(savedInstanceState);

    mDrawerToggle.syncState();

}

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

public void onBackPressed() {

    mDrawerLayout.closeDrawer(mDrawerList);
}
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
伊尼亚巴科斯PerfilAdapter.iniciarbastatos(本);
perfilObj=PerfilAdapter。选择perfil(1);
requestWindowFeature(窗口.功能\u操作\u栏);
cargarActionBar();
cargarDrawerLayout(savedInstanceState);
}
@凌驾
公共布尔onCreateOptions菜单(菜单){
getMenuInflater().充气(R.menu.menu,menu);
返回true;
}
@凌驾
公共布尔值onOptionsItemSelected(菜单项项){
如果(MDRAWERTOGLE.onOptionsItemSelected(项目)){
返回true;
}
开关(item.getItemId()){
案例R.id.menuOpcSonidos:
...
返回true;
案例R.id.MENUOPCABIARCOLOR:
...
返回true;
违约:
返回super.onOptionsItemSelected(项目);
}
}
@凌驾
公共布尔值OnPrepareOptions菜单(菜单){
menu.findItem(R.id.menuOpcSonidos)
.setTitle(getResources().getString(R.string.sonidoOnOff)+“”+perfilObj.getSonidos());
返回super.onPrepareOptions菜单(菜单);
}
私有void cargarActionBar(){
ActionBar ActionBar=getActionBar();
int[]colores2=Modulo.cargarColoresDrawerlayout(perfilObj.getColor());
actionBar.setBackgroundDrawable(新渐变Drawable(Orientation.BOTTOM_-TOP,彩色2));
getActionBar().setDisplayHomeAsUpEnabled(true);
getActionBar().setHomeButtonEnabled(true);
int titleId=Resources.getSystem().getIdentifier(“操作栏标题”、“id”、“android”);
TextView textoTitulo=(TextView)findViewById(titleId);
setTextColor(getResources().getColor(R.color.blanco));
text otitulo.setTypeface(null,Typeface.BOLD);
textoTitulo.setTextSize(19);
setShadowLayer(5,0,0,getResources().getColor(R.color.negro));
}
私人空旷区cargarDrawerLayout(b包){
mTitle=mDrawerTitle=getTitle();
textosMenuLateral=getResources().getStringArray(R.array.nav\u抽屉\u项目);
iconosMenuLateral1=getResources()
.obtainTypedArray(R.array.iconos\u menu\u lateral1);
iconosMenuLateral2=getResources()
.obtainTypedArray(R.array.iconos\u menu\u lateral2);
mDrawerLayout=(抽屉布局)findViewById(R.id.抽屉布局);
mDrawerList=(ListView)findViewById(R.id.list\u slidemenumain);
int[]颜色={0,0xFFFFFFFF,0};
mDrawerList.setDivider(新的GradientDrawable(Orientation.RIGHT_LEFT,colors));
mDrawerList.setDividerHeight(4);
navDrawerItems1=新的ArrayList();
navDrawerItems1.add(新的DrawerItem(textosMenuLateral[0],iconosMenuLateral1.getResourceId(0,-1));
添加(新的DroperItem(textosMenuLateral[1],iconosMenuLateral1.getResourceId(1,-1));
navDrawerItems1.add(新的DrawerItem(textosMenuLateral[2],iconosMenuLateral1.getResourceId(2,-1));
navDrawerItems1.add(新的DrawerItem(textosMenuLateral[3],iconosMenuLateral1.getResourceId(3,-1));
navDrawerItems1.add(新的DrawerItem(textosMenuLateral[4],iconosMenuLateral1.getResourceId(4,-1));
navDrawerItems2=新的ArrayList();
添加(新的DroperItem(textosMenuLateral[0],iconosMenuLateral2.getResourceId(0,-1));
添加(新的DroperItem(textosMenuLateral[1],iconosMenuLateral2.getResourceId(1,-1));
添加(新的DroperItem(textosMenuLateral[2],iconosMenuLateral2.getResourceId(2,-1));
添加(新的DroperItem(textosMenuLateral[3],iconosMenuLateral2.getResourceId(3,-1));
添加(新的DroperItem(textosMenuLateral[4],iconosMenuLateral2.getResourceId(4,-1));
iconosMenuLateral1.recycle();
iconosMenuLateral2.recycle();
setOnItemClickListener(新的SlideMenuClickListener());
adapter=新的抽屉式StatAdapter(getApplicationContext(),
导航系统MS1,
NAVS2,
perfilObj.getColor(),
pos);
mDrawerList.setAdapter(适配器);
mDrawerToggle=新操作bardrawertoggle(
这
mDrawerLayout,
R.drawable.icono_抽屉,
R.string.app_名称,
R.string.app\u名称
) {
公共无效onDrawerClosed(视图){
getActionBar().setTitle(mTitle);
无效操作菜单();
} 
打开图纸上的公共空白(视图抽屉视图){
getActionBar().setTitle(mDrawerTitle);
无效操作菜单();
}
};
mDrawerLayout.setDrawerListener(mDrawerToggle);
如果(b==null){
视神经(0);
}
}
私有类SlideMenuClickListener实现ListView.OnItemClickListener{
@凌驾
public void onItemClick(AdapterView父视图、视图、整型位置、,
长id){
TextView TextView=(TextView)view.findViewById(R.id.title);
textView.setTypeface(null,Typeface.BOLD);
视神经(位置);
}
}
私人void OpcionesPanel横向(内部位置){
意图一;
开关(位置){
案例0:
pos=0;
打破
案例1:
i=新意图(此,ActivitySecond.class);
mDrawerLayout.closeDrawer(mDrawerList);
星触觉(i);
pos=1;
打破
案例2:
i=新意图(本活动为第三类);
mDrawerLayout.closeDrawer(mDrawerList);
星触觉(i);
pos=2;
打破
案例3:
i=新意图(本活动为第四类);
mDrawerLayout.closeDrawer(mDrawerList);
startActivi
package my_package;
//------------------------------------------------------------------------------
import android.app.Activity;
import android.graphics.drawable.ColorDrawable;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.support.v4.widget.DrawerLayout;
//------------------------------------------------------------------------------
public class menu_act extends    ActionBarActivity
                      implements drawer_frg.NavigationDrawerCallbacks {
//------------------------------------------------------------------------------
// Fragment managing the behaviors, interactions and presentation of the navigation drawer.
private drawer_frg mNavigationDrawerFragment;
// Used to store the last screen title. For use in {@link #restoreActionBar()}.
private CharSequence mTitle;
//------------------------------------------------------------------------------
@Override
protected void onCreate ( Bundle savedInstanceState ) {
super.onCreate( savedInstanceState );
setContentView( R.layout.menu_lay );

mNavigationDrawerFragment = (drawer_frg) getSupportFragmentManager().findFragmentById(
                                                       R.id.navigation_drawer );
mTitle = "Perfil"; // getTitle();

// Set up the drawer.
mNavigationDrawerFragment.setUp( R.id.navigation_drawer,( DrawerLayout )
                                           findViewById( R.id.drawer_layout ) );

// BARRA DE TÍTULO ANARANJADA.
//ActionBar actionBar = getSupportActionBar();
//actionBar.setBackgroundDrawable( new ColorDrawable( getResources().getColor( R.color.col_nar ) ) );
}
//------------------------------------------------------------------------------
@Override
public void onNavigationDrawerItemSelected ( int position ) {
Fragment frg;
// getSupportActionBar().setDisplayOptions( ActionBar.DISPLAY_SHOW_CUSTOM );
switch ( position )
{ case 0  : //getSupportActionBar().setCustomView( R.layout.perfil_tit );
            mTitle = "Perfil";
            frg = new perfil_frg();
            break;
  case 1  : // getSupportActionBar().setCustomView( R.layout.contactos_tit );
            mTitle = "Contactos";
            frg = new contactos_frg();
            break;
  default : frg = PlaceholderFragment.newInstance( position + 1 );
}
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction().replace( R.id.container,frg ).commit();
}
//------------------------------------------------------------------------------
public void onSectionAttached ( int number ) {
switch (number) {
  case 1 : mTitle = getString( R.string.mnu_opc_per ); break;
  case 2 : mTitle = getString( R.string.mnu_opc_con ); break;
  case 3 : mTitle = getString( R.string.mnu_opc_sal ); break;
}
}
//------------------------------------------------------------------------------
public void restoreActionBar () {
ActionBar actionBar = getSupportActionBar();
actionBar.setNavigationMode( ActionBar.NAVIGATION_MODE_STANDARD );
actionBar.setDisplayShowTitleEnabled( true );
actionBar.setTitle( mTitle );
}
//------------------------------------------------------------------------------
@Override
public boolean onCreateOptionsMenu(Menu menu) {
if ( ! mNavigationDrawerFragment.isDrawerOpen() )
   { // Only show items in the action bar relevant to this screen
     // if the drawer is not showing. Otherwise, let the drawer
     // decide what to show in the action bar.
     getMenuInflater().inflate( R.menu.menu_act,menu );
     restoreActionBar();
     return true;
   }
return super.onCreateOptionsMenu( menu );
}
//------------------------------------------------------------------------------
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
/*
//noinspection SimplifiableIfStatement
if ( id == R.id.action_settings )
     return true;
*/
//mDrawerToggle.syncState();
return super.onOptionsItemSelected( item );
}
//==============================================================================
// A placeholder fragment containing a simple view.
public static class PlaceholderFragment extends Fragment {
//------------------------------------------------------------------------------
// The fragment argument representing the section number for this fragment.
private static final String ARG_SECTION_NUMBER = "section_number";
//------------------------------------------------------------------------------
// Returns a new instance of this fragment for the given section number.
public static PlaceholderFragment newInstance(int sectionNumber) {
PlaceholderFragment fragment = new PlaceholderFragment();
Bundle args = new Bundle();
args.putInt(ARG_SECTION_NUMBER, sectionNumber);
fragment.setArguments(args);
return fragment;
}
//------------------------------------------------------------------------------
public PlaceholderFragment() {
}
//------------------------------------------------------------------------------
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                       Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.menu_frg, container, false);
return rootView;
}
//------------------------------------------------------------------------------
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
((menu_act) activity).onSectionAttached( getArguments().getInt(ARG_SECTION_NUMBER));
}
//==============================================================================
}
//------------------------------------------------------------------------------
}