如何使用工具栏而不是操作栏将旧的导航抽屉片段代码升级为代码?

问题描述

1.Toolbar以XML声明并包含在有关的XML

    代码(在底部)中给出的
  1. restoreActionbar函数按照Actionbar到Toolbar的净指令进行了中间化处理。
  2. 这是前面为菜单选项提供的“标准导航抽屉片段”代码。有示例代码可将其从操作栏升级到工具栏。
  3. 首先尝试使用OnCreate方法(在以下代码中注释)-不起作用,试图在全局中声明它(在以下代码中注释)不起作用。 请帮忙

公共类MainActivity1扩展了AppCompatActivity实现 NavigationDrawerFragment.NavigationDrawerCallbacks {

/**
 * Fragment managing the behaviors,interactions and presentation of the
 * navigation drawer.
 */
private NavigationDrawerFragment mNavigationDrawerFragment;

private ShareActionProvider mShareActionProvider;

/**
 * Used to store the last screen title. For use in
 * {@link #restoreActionBar()}.
 */
private CharSequence mTitle;
//private int mPosition = 0;
private String mMessage;
//private int mSectionNo =0;
private String mMenuType="";
private Random r1 = new Random();
private Random r2 = new Random();
Globals g = Globals.getInstance();
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    
    //Globals g = Globals.getInstance();
    //mSectionNo = g.getData();
    mMenuType = g.getMenuType();
    
    //Intent intent = getIntent();
    //mMessage = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
    
    setContentView(R.layout.activity_main_activity1);
     
    mNavigationDrawerFragment = (NavigationDrawerFragment) getSupportFragmentManager()
            .findFragmentById(R.id.navigation_drawer);
    mTitle = getTitle();
    /*if (mMessage.equals("1")){
        mNavigationDrawerFragment.mSectionNo = 1;
    }*/

    // Set up the drawer.
    mNavigationDrawerFragment.setUp(R.id.navigation_drawer,(DrawerLayout) findViewById(R.id.drawer_layout));
    
    SharedPreferences sharedPreferences = getPreferences(Context.MODE_PRIVATE);

       int radioId=sharedPreferences.getInt("shapecheck",0);
       if(radioId>0){
       g.mShapeOption = 1;  
       }
    //Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    //setSupportActionBar(toolbar);
}

@Override
public void onNavigationDrawerItemSelected(int position) {
    // update the main content by replacing fragments
    //mPosition = position;
    Globals g = Globals.getInstance();
    g.setSectionType(position);
    
    if ((mMenuType.equals("2to10"))||(mMenuType.equals("11to20"))||(mMenuType.equals("21to30")))
            {
            if(position==10){
            this.finish();} // to exit
    }
    if ((mMenuType.equals("randomizer")) ||(mMenuType.equals("sequential"))||(mMenuType.equals("quiz"))||(mMenuType.equals("query"))){
        if(position==4){
        this.finish();
    }
    }
    if ((mMenuType.equals("about")) || (mMenuType.equals("shapeoption"))){
        if(position==1){
        this.finish();
    }
    }
    FragmentManager fragmentManager = getSupportFragmentManager();
    fragmentManager
            .beginTransaction()
            .replace(R.id.container,PlaceholderFragment.newInstance(position + 1)).commit();
    //TextView txtView1 = (TextView) findViewById(R.id.TextView79);
    //txtView1.setBackgroundResource(R.drawable.nine);
}

public void onSectionAttached(int number) {
    switch (number) {
    case 1:
        case1Selection();
        break;
    case 2:
        case2Selection();
        break;
    case 3:
        case3Selection();
        break;
    case 4:
        case4Selection();
        break;
    case 5:
        case5Selection();
        break;
    case 6:
        case6Selection();
        break;
    case 7:
        case7Selection();
        break;
    case 8:
        case8Selection();
        break;
    case 9:
        case9Selection();
        break;
    case 10:
        case10Selection();
        break;
    case 11:
        case11Selection();
        break;
    }
}

public void restoreActionBar() {
    //Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    //Toolbar toolbar getSupportActionBar();
    //toolbar.setnavigationMode(Toolbar.NAV)
    //ActionBar actionBar = getSupportActionBar();

    //actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
    //actionBar.setdisplayShowTitleEnabled(true);
    toolbar.setTitle(mTitle);
    //actionBar.setTitle(mTitle);
}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)