【问题标题】:Extended navigation drawer activity扩展的导航抽屉活动
【发布时间】:2014-04-02 00:58:31
【问题描述】:

我有活动,我们称之为 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/

【问题讨论】:

    标签: android navigation-drawer


    【解决方案1】:

    我认为您想要的是拥有一个使用ActionBarDrawerToggleActivity 和另一个仍然使用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) 一个实现ActionBarDrawerToggleActivity

    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);
        }
    
    }
    

    当您想要使用DrawerLayout 不带 切换时,子类BaseDrawerActivity 并且当您想要使用Activity 带有 切换时,子类@ 987654333@。因此,您的 TestActivity 将如下所示:

    public class TestActivity extends BaseDrawerActivity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            getActionBar().setDisplayHomeAsUpEnabled(true);
        }
    
    }
    

    【讨论】:

    • 谢谢,这就是我要找的。​​span>
    猜你喜欢
    • 2014-05-04
    • 2014-04-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-02
    • 1970-01-01
    • 2014-05-03
    • 1970-01-01
    相关资源
    最近更新 更多