gpt4 book ai didi

android - 如何为没有工具栏的 ActionBarDrawerToggle v7 设置抽屉切换图标 (`hamburger`)

转载 作者:行者123 更新时间:2023-11-29 15:12:02 26 4
gpt4 key购买 nike

我正在使用 android.support.v7.app.ActionBarActivity 中的操作栏,所以我的 Activity 声明如下:

public class MyActivity extends ActionBarActivity {

我正在阅读说明如何添加抽屉的教程:

DrawerLayout drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawerLayout, R.drawable.ic_drawer, R.string.drawer_open, R.string.drawer_close);

这是为了 import android.support.v4.app.ActionBarDrawerToggle。现在我决定使用 v7,因为 v4 已被弃用,但 v7 的构造函数不接受抽屉图标。我用谷歌搜索,发现解决方案是使用接受 toolbar 的构造函数风格:

ActionBarDrawerToggle(this, drawerLayout, Toolbar toolbar, int openDrawerContentDescRes, int closeDrawerContentDescRes)

但我不使用工具栏。所以我不明白如何在没有工具栏的情况下使用 v7 设置图标。我应该改用工具栏吗?

最佳答案

嘿,你在这里找到了解决方案..

private Toolbar toolbar;
toolbar = (Toolbar) findViewById(R.id.toolbar);

if (toolbar != null) {
setSupportActionBar(toolbar);
toolbar.setNavigationIcon(R.drawable.ic_drawer);

mDrawerToggle = new ActionBarDrawerToggle(this,
mDrawerLayout,
toolbar,
R.string.drawer_open,
R.string.drawer_close) {

/** Called when a drawer has settled in a completely closed state. */
public void onDrawerClosed(View view) {
super.onDrawerClosed(view);
}

/** Called when a drawer has settled in a completely open state. */
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
}
};

// Set the drawer toggle as the DrawerListener
mDrawerLayout.setDrawerListener(mDrawerToggle);
mDrawerToggle.syncState();

getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
}

这也是一个很好的方法,我个人使用这个。

public class MyActionBarDrawerToggle extends android.support.v7.app.ActionBarDrawerToggle {

public MyActionBarDrawerToggle(Activity activity, final DrawerLayout drawerLayout, Toolbar toolbar, int openDrawerContentDescRes, int closeDrawerContentDescRes) {
super(activity, drawerLayout, toolbar, openDrawerContentDescRes, closeDrawerContentDescRes);

setHomeAsUpIndicator(R.drawable.drawer_toggle);
setDrawerIndicatorEnabled(false);

setToolbarNavigationClickListener(new View.OnClickListener() {

@Override
public void onClick(View view) {
drawerLayout.openDrawer(Gravity.LEFT);
}
});
}
}

谢谢。

关于android - 如何为没有工具栏的 ActionBarDrawerToggle v7 设置抽屉切换图标 (`hamburger`),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30370886/

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