Android actionbar按钮,android - Android-将ActionBar后退按钮切换到导航按钮

我遇到以下问题:

我知道如何设置工具栏以显示后退按钮图标而不是汉堡按钮图标。

由此:

Sx26a.png

对此:

dF12T.png

使用:getSupportActionBar().setDisplayHomeAsUpEnabled(true);

现在,我要执行相反的操作,我要从“后退”按钮图标转到“汉堡”图标:

dF12T.png

到这里:

Sx26a.png

我怎样才能做到这一点?

更新:@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setSupportActionBar(mToolbar);

getSupportActionBar().setDisplayShowTitleEnabled(false);

}

private void enableViews(boolean enable) {

if(enable) {

// Enables back button icon

getSupportActionBar().setDisplayHomeAsUpEnabled(true);

} else {

// TODO: Enables burger icon

}

}

最佳答案

如果我假设您在布局中使用android.support.v4.widget.DrawerLayout,则此方法可能对您有效;我仅在API 21上进行了测试,但是鉴于它主要使用了支持库,因此它应该在较低或较高的目标上都可以工作(著名的最后一句话)。import android.support.v7.app.ActionBarDrawerToggle

import android.support.v4.widget.DrawerLayout

ActionBarDrawerToggle mDrawerToggle;

DrawerLayout drawerLayout;

private boolean mToolBarNavigationListenerIsRegistered = false;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setSupportActionBar(mToolbar);

getSupportActionBar().setDisplayShowTitleEnabled(false);

// Get DrawerLayout ref from layout

drawerLayout = (DrawerLayout)findViewById(R.id.drawer);

// Initialize ActionBarDrawerToggle, which will control toggle of hamburger.

// You set the values of R.string.open and R.string.close accordingly.

// Also, you can implement drawer toggle listener if you want.

mDrawerToggle = new ActionBarDrawerToggle (this, drawerLayout, mToolbar, R.string.open, R.string.close);

// Setting the actionbarToggle to drawer layout

drawerLayout.addDrawerListener(mDrawerToggle);

// Calling sync state is necessary to show your hamburger icon...

// or so I hear. Doesn't hurt including it even if you find it works

// without it on your test device(s)

mDrawerToggle.syncState();

}

/**

* To be semantically or contextually correct, maybe change the name

* and signature of this function to something like:

*

* private void showBackButton(boolean show)

* Just a suggestion.

*/

private void enableViews(boolean enable) {

// To keep states of ActionBar and ActionBarDrawerToggle synchronized,

// when you enable on one, you disable on the other.

// And as you may notice, the order for this operation is disable first, then enable - VERY VERY IMPORTANT.

if(enable) {

//You may not want to open the drawer on swipe from the left in this case

drawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);

// Remove hamburger

mDrawerToggle.setDrawerIndicatorEnabled(false);

// Show back button

getSupportActionBar().setDisplayHomeAsUpEnabled(true);

// when DrawerToggle is disabled i.e. setDrawerIndicatorEnabled(false), navigation icon

// clicks are disabled i.e. the UP button will not work.

// We need to add a listener, as in below, so DrawerToggle will forward

// click events to this listener.

if(!mToolBarNavigationListenerIsRegistered) {

mDrawerToggle.setToolbarNavigationClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

// Doesn't have to be onBackPressed

onBackPressed();

}

});

mToolBarNavigationListenerIsRegistered = true;

}

} else {

//You must regain the power of swipe for the drawer.

drawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED);

// Remove back button

getSupportActionBar().setDisplayHomeAsUpEnabled(false);

// Show hamburger

mDrawerToggle.setDrawerIndicatorEnabled(true);

// Remove the/any drawer toggle listener

mDrawerToggle.setToolbarNavigationClickListener(null);

mToolBarNavigationListenerIsRegistered = false;

}

// So, one may think "Hmm why not simplify to:

// .....

// getSupportActionBar().setDisplayHomeAsUpEnabled(enable);

// mDrawer.setDrawerIndicatorEnabled(!enable);

// ......

// To re-iterate, the order in which you enable and disable views IS important #dontSimplify.

}

该解决方案使用ActionBarDrawerToggle.setDrawerIndicatorEnabled切换汉堡包图标的可见性,并使用ActionBar.setDisplayHomeAsUpEnabled切换“向上”按钮的可见性,实质上是利用它们各自的drawable资源。

其他假设

您的 Activity 主题扩展了Theme.AppCompat.Light.NoActionBar。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值