gpt4 book ai didi

android - 单击应用程序图标打开抽屉导航

转载 作者:塔克拉玛干 更新时间:2023-11-02 07:52:33 24 4
gpt4 key购买 nike

我想让我的用户通过单击应用程序图标来打开抽屉导航。这是我的代码:

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add);
// Show the Up button in the action bar.

DrawerLayout mDrawerLayout = (DrawerLayout) findViewById(R.id.left_drawer);
ActionBarDrawerToggle mDrawerToggle = new ActionBarDrawerToggle(
this, /* host Activity */
mDrawerLayout, /* DrawerLayout object */
R.drawable.ic_drawer, /* nav drawer icon to replace 'Up' caret */
R.string.drawer_open, /* "open drawer" description */
R.string.drawer_close /* "close drawer" description */
) {

/** Called when a drawer has settled in a completely closed state. */
public void onDrawerClosed(View view) {
getActionBar().setTitle(R.string.title_activity_add);
}

/** Called when a drawer has settled in a completely open state. */
public void onDrawerOpened(View drawerView) {
getActionBar().setTitle(R.string.drawer_title);
}
};


mDrawerLayout.setDrawerListener(mDrawerToggle);
getActionBar().setDisplayHomeAsUpEnabled(true); // Pressing the app icon in the action bar will navigate to the parent activity.
getActionBar().setHomeButtonEnabled(true);

}

但是当我点击图标时,没有任何反应。哪里出了问题。

最佳答案

在这里查看 example of the docs .您需要在

中添加其他代码
  • onPostCreate() 同步你的抽屉状态
  • onOptionsItemSelected() 处理App图标的触摸事件
  • onConfigurationChanged() 为抽屉提供新配置

    public class YourActivity extends Activity {

    public ActionBarDrawerToggle mDrawerToggle;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    ...
    mDrawerToggle = new ActionBarDrawerToggle();
    ...
    }

    @Override
    protected void onPostCreate(Bundle savedInstanceState) {
    super.onPostCreate(savedInstanceState);
    // Sync the toggle state after onRestoreInstanceState has occurred.
    mDrawerToggle.syncState();
    }

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

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
    // Pass the event to ActionBarDrawerToggle, if it returns
    // true, then it has handled the app icon touch event
    if (mDrawerToggle.onOptionsItemSelected(item)) {
    return true;
    }
    // Handle your other action bar items...

    return super.onOptionsItemSelected(item);
    }

    }

关于android - 单击应用程序图标打开抽屉导航,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19889436/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com