setTitle(menuItem.getTitle())就无法工作。当我尝试从一个片段改变或跳转到那个时,动作栏只设置了应用名称。< / p>
public class MainActivity extends AppCompatActivity {
Toolbar mToolBar;
NavigationView mNavigationView;
DrawerLayout mDrawerLayout;
ActionBarDrawerToggle mActionBarDrawerToggle;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if(savedInstanceState == null){
getSupportFragmentManager().beginTransaction().replace(R.id.containerView,new HomeFragment()).commit();
}
mToolBar = (Toolbar)findViewById(R.id.toolbar);
setSupportActionBar(mToolBar);
mDrawerLayout = (DrawerLayout)findViewById(R.id.drawerLayout);
mNavigationView = (NavigationView)findViewById(R.id.navigationDrawer);
mActionBarDrawerToggle = setupDrawerToggle();
mDrawerLayout.addDrawerListener(mActionBarDrawerToggle);
setupDrawerContent(mNavigationView);
}
private ActionBarDrawerToggle setupDrawerToggle(){
return new ActionBarDrawerToggle(this,mDrawerLayout,mToolBar,R.string.drawer_open,R.string.drawer_close);
}
private void setupDrawerContent(NavigationView mNavigationView){
mNavigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(MenuItem menuItem) {
selectDrawerItem(menuItem);
return true;
}
});
}
public void selectDrawerItem(MenuItem menuItem){
Fragment fragment = null;
Class fragmentClass;
switch (menuItem.getItemId()){
case R.id.nav_home:
fragmentClass = HomeFragment.class;
break;
case R.id.nav_rate_card:
fragmentClass = RateCardFragment.class;
break;
case R.id.nav_orders:
fragmentClass = OrderFragment.class;
break;
case R.id.nav_setting:
fragmentClass = SettingFragment.class;
break;
case R.id.nav_about_us:
fragmentClass = AboutUsFragment.class;
break;
default:
fragmentClass = HomeFragment.class;
}
try{
fragment = (Fragment) fragmentClass.newInstance();
}catch (Exception ex){
ex.printStackTrace();
}
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction().replace(R.id.containerView,fragment).commit();
// Highlight the selected item has been done by NavigationView
//menuItem.setChecked(true);
// Set action bar title
setTitle(menuItem.getTitle());
mDrawerLayout.closeDrawers();
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// The action bar home/up action should open or close the drawer.
switch (item.getItemId()) {
case android.R.id.home:
mDrawerLayout.openDrawer(GravityCompat.START);
return true;
}
if (mActionBarDrawerToggle.onOptionsItemSelected(item)){
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
mActionBarDrawerToggle.syncState();
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
// Pass any configuration change to the drawer toggles
mActionBarDrawerToggle.onConfigurationChanged(newConfig);
}
}
答案 0 :(得分:1)
使用getSupportActionBar().setTitle()
并尝试。
答案 1 :(得分:0)
在toolbar.xml中取一个textview并访问它。就像这个
if(waist <= 0 || height <= 0)
if (waist <= 0) {
MessageBox.Show("Please enter a number that greater than 60");
}
if (height <=0 ) {
MessageBox.Show("Please enther a number that greater than 120");
}