我有活动,可以称之为TestActivity。这个TestActivity扩展了具有导航抽屉的Activity。在TestActivity中,我想更改NavigationDrawerToggle并将其替换为向上箭头。

public class Test extends DashboardActivity {

    private DrawerLayout navDrawerLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        navDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);

        getActionBar().setDisplayHomeAsUpEnabled(true);

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.test, menu);
        return true;
    }

    @Override
    public void onBackPressed() {
        this.finish();
        super.onBackPressed();
    }

}


仪表板活动相当长,并且那里还有更多内容,但基本上它遵循本教程:http://www.androidhive.info/2013/11/android-sliding-menu-using-navigation-drawer/

最佳答案

我认为您想要的是拥有一个使用ActivityActionBarDrawerToggle和另一个仍然使用DrawerLayout的但没有切换的Activity。在这种情况下,最简单的方法是拥有两个“基础”类。这是一个例子。

1)实现DrawerLayoutActivity

public class BaseDrawerActivity extends Activity {

    protected DrawerLayout mDrawer;

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

        mDrawer = ...
    }

}


2)实现ActionBarDrawerToggleDrawerLayout

public class BaseDrawerToggleActivity extends BaseDrawerActivity {

    private ActionBarDrawerToggle mDrawerToggle;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mDrawerToggle = ...
        mDrawer.setDrawerListener(mDrawerToggle);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (mDrawerToggle.onOptionsItemSelected(item)) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    protected void onPostCreate(Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);
        mDrawerToggle.syncState();
    }

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

}


如果要在没有切换的情况下使用BaseDrawerActivity子类,请在Activity中使用,而在要切换的情况下使用BaseDrawerToggleActivity子类TestActivity。因此,您的将如下所示:

public class TestActivity extends BaseDrawerActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getActionBar().setDisplayHomeAsUpEnabled(true);
    }

}

08-27 16:19