2016-07-18 4 views
0

내 응용 프로그램에서 다음과 같은 탐색보기가 있습니다. 사용자가 메뉴 항목 Καλώς ήρθες (그리스어의 의미 환영)을 클릭하면탐색 항목 메뉴를 클릭 할 때 도구 모음의 제목 설정

my app

그래서, 나는이 도구 모음에 표시하고자합니다. 이것은 내비게이션 뷰의 코드입니다.

public class Welcome extends AppCompatActivity { 
private NavigationView navigationView; 
private DrawerLayout drawerLayout; 
private SessionManager session; 
Toolbar toolbar; 
private int mIconSize; 
private String username; 


@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_welcome); 

    session = new SessionManager(getApplicationContext()); 



    SharedPreferences userName = PreferenceManager.getDefaultSharedPreferences(this); 
    //UserId = userName.getString("id",""); 
    username = userName.getString("user_name", ""); 



    toolbar = (Toolbar)findViewById(R.id.toolbar); 
    setSupportActionBar(toolbar); 

    drawerLayout = (DrawerLayout)findViewById(R.id.drawer_layout); 

    if (savedInstanceState == null) { 
     //Restore the fragment's instance and geo coordinates! 
     //homeFragment = (FragmentHome) getSupportFragmentManager().getFragment(savedInstanceState, "mContent"); 
     setSelected(R.id.welcome); 
    }else{ 
     //profileFragment = (FragmentProfile) fm2.findFragmentByTag(PROFILE_FRAGMENT); 

    } 
    ActionBarDrawerToggle adt = new ActionBarDrawerToggle(this,drawerLayout,toolbar, 
      R.string.open_drawer,R.string.close_drawer){ 
     @Override 
     public void onDrawerClosed(View drawerView) { 
      super.onDrawerClosed(drawerView); 
     } 

     @Override 
     public void onDrawerOpened(View drawerView) { 
      super.onDrawerOpened(drawerView); 
     } 
    }; 

    drawerLayout.addDrawerListener(adt); 
    adt.syncState(); 
    navigationView = (NavigationView)findViewById(R.id.navigation_view); 
    navigationView.setBackgroundColor(getResources().getColor(R.color.navigation_bg_color)); 
    navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() { 
     @Override 
     public boolean onNavigationItemSelected(MenuItem item) { 
      drawerLayout.closeDrawers(); 
      boolean result = setSelected(item.getItemId()); 
      return result; 
     } 
    }); 

    View v = navigationView.getHeaderView(0); 
    TextView t = (TextView)v.findViewById(R.id.username); 
    t.setText(username); 
} 
@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    MenuInflater inflater = getMenuInflater(); 
    inflater.inflate(R.menu.log_out, menu); 
    return true; 
} 
@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    switch (item.getItemId()) { 
     case R.id.log_out: 

      logoutUser(); 
      return true; 
    } 
    return super.onOptionsItemSelected(item); 
} 
private void logoutUser() { 
    session.setLogin(false); 

    // Launching the login activity 
    Intent intent = new Intent(Welcome.this, MainActivity.class); 
    startActivity(intent); 
    finish(); 
} 
public boolean setSelected(int selected) { 
    switch (selected){ 
     case R.id.welcome: 
      WelcomeFragment welcomeFragment = new WelcomeFragment(); 
      FragmentManager fm1 = getSupportFragmentManager(); 
      FragmentTransaction fragmentTra1 = fm1.beginTransaction(); 
      fragmentTra1.replace(R.id.content,welcomeFragment); 
      //fragmentTra1.addToBackStack("added"); 

      fragmentTra1.commit(); 
     return true; 
     case R.id.general: 
      GeneralFragment generalFragment= new GeneralFragment(); 
      FragmentManager fm2 = getSupportFragmentManager(); 
      FragmentTransaction fragmentTra2 = fm2.beginTransaction(); 
      fragmentTra2.replace(R.id.content,generalFragment); 
      //fragmentTra1.addToBackStack("added"); 

      fragmentTra2.commit(); 
      return true; 
     case R.id.announcements: 
      AnnouncementsFragment announcementsFragment= new AnnouncementsFragment(); 
      FragmentManager fm3 = getSupportFragmentManager(); 
      FragmentTransaction fragmentTraSubOne = fm3.beginTransaction(); 
      fragmentTraSubOne.replace(R.id.content,announcementsFragment); 
      //fragmentTra1.addToBackStack("added"); 

      fragmentTraSubOne.commit(); 
      return true; 
     case R.id.sub_one: 
      NewsFragment newsFragment= new NewsFragment(); 
      FragmentManager fm4 = getSupportFragmentManager(); 
      FragmentTransaction fragmentTraSubTwo = fm4.beginTransaction(); 
      fragmentTraSubTwo.replace(R.id.content,newsFragment); 
      //fragmentTra1.addToBackStack("added"); 

      fragmentTraSubTwo.commit(); 
      return true; 
     case R.id.sub_two: 
      FanClubFragment fanClubFragment= new FanClubFragment(); 
      FragmentManager fm5 = getSupportFragmentManager(); 
      FragmentTransaction fragmentTraSubThree = fm5.beginTransaction(); 
      fragmentTraSubThree.replace(R.id.content,fanClubFragment); 
      //fragmentTra1.addToBackStack("added"); 
      fragmentTraSubThree.commit(); 
      return true; 
    } 

    return false; 
} 
public static Intent newIntent(Context context) { 

    return new Intent(context,MainActivity.class); 
} 
@Override 
public void onBackPressed() { 
    if(isNavDrawerOpen()){ 
     closeDrawerLayout(); 
    }else { 
     super.onBackPressed(); 
    } 
} 

public boolean isNavDrawerOpen() { 
    return drawerLayout!=null && drawerLayout.isDrawerOpen(GravityCompat.START); 
} 
private void closeDrawerLayout(){ 
    if(drawerLayout != null){ 
     drawerLayout.closeDrawer(GravityCompat.START); 
    } 
} 
@Override 
public boolean onKeyDown(int keyCode, KeyEvent event) 
{ 
    return (keyCode == KeyEvent.KEYCODE_BACK ? true :  
    super.onKeyDown(keyCode, event)); 
} 
} 

아이디어가 있으십니까?

감사합니다.

테오.

답변

2

setTitle() 사용해보십시오 :

public boolean setSelected(int selected) { 
    switch (selected){ 
      case R.id.welcome: 
       toolbar.setTitle("Welcome"); 
       ...... 
      } 
    ..... 
} 
2

사용해보십시오 :

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    MenuItem item = menu.add(Menu.NONE, ID, POSITION, TEXT); 
    item.setIcon(R.drawable.drawable_resource_name); 
    item.setShowAsAction(MenuItem.SHOW_AS_ACTION_WITH_TEXT); 

    return true; 
} 

: 스위치의 경우에

getSupportActionBar().setTitle("title"); 

+0

@Harsh Pandey가 setTitle ("title")이라고 직접 말하거나 사용합니다. – thunder413

0

난 당신이 뭔가를 사용할 수 있다고 생각 또한 th 링크는 도움이 될 수있다 : Here

는 희망이 도움)

이 BTW 앱이 멋지다!

관련 문제