![](/img/trans.png)
[英]How to change the color of ActionBarToggle in android while using toolbar in android lollipop?
[英]How to replace the hamburger icon used for ActionBarToggle on Android Toolbar with a custom drawable?
我已經使用Android 5.0中的新工具欄實現了一個基本的ActionBarDrawerToggle。
但是,我無法弄清楚如何更改提供的默認漢堡包圖標。 從android文檔中可以看出“給定的Activity將鏈接到指定的DrawerLayout,工具欄的導航圖標將設置為自定義drawable ...這個drawable在抽屜關閉時顯示漢堡圖標,在抽屜打開時顯示箭頭當抽屜打開時,它會在這兩個狀態之間激活。“
我目前使用以下代碼正常工作,但是我想用我自己的drawable替換默認提供的漢堡包。
這是我目前的代碼:
MainActivity.java
@InjectView(R.id.main_activity_toolbar)
Toolbar mToolbar;
@InjectView(R.id.main_activity_drawer_layout)
DrawerLayout mDrawerLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.main_activity);
super.onCreate(savedInstanceState);
setSupportActionBar(mToolbar);
mToolbar.setNavigationIcon(R.drawable.navigation);
mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, mToolbar, R.string.drawer_open, R.string.drawer_close) {
public void onDrawerClosed(View view) {
super.onDrawerClosed(view);
invalidateOptionsMenu();
}
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
invalidateOptionsMenu();
}
};
mDrawerLayout.setDrawerListener(mDrawerToggle);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
}
這一行:
mToolbar.setNavigationIcon(R.drawable.navigation);
似乎不起作用。
這可能嗎? 謝謝!
ActionBarToggle文檔 - https://developer.android.com/reference/android/support/v7/app/ActionBarDrawerToggle.html
這兩行代碼對我有用:
mDrawerToggle.setDrawerIndicatorEnabled(false); //disable "hamburger to arrow" drawable
mDrawerToggle.setHomeAsUpIndicator(R.drawable.ic_drawer); //set your own
然后打電話給:
mDrawerToggle.syncState();
您可以使用工具欄作為單機模式下,這意味着你不應該用你的工具欄為您ActionBarDrawerToggle構造的一部分,就可以實現,使用下面的代碼:
mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, null,
R.drawable.appbar, R.drawable.appbar)
(注意工具欄實例未如何發送到ActionBarDrawerToggle構造函數)
此外,您應手動充氣菜單
mToolbar = (Toolbar) findViewById(R.id.nav_toolbar);
mToolbar.inflateMenu(R.menu.base);
並刪除 setSupportActionBar(mToolbar); 代碼行。
當然,您必須自己處理導航點擊:
mToolbar.setOnMenuItemClickListener(new OnMenuItemClickListener() ...
然后,您可以這樣打開您的抽屜:
drawerButton = (BadgeDrawerButton) findViewById(R.id.badge_drawer_button);
drawerButton.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
mDrawerLayout.openDrawer(Gravity.LEFT);
}
});
希望這可能有所幫助。
我的解決方案是通過繼承ActionBarDrawerToggle。
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);
}
});
}
}
ActionBarDrawerToggle的默認菜單是DrawerArrowDrawable。
您可以將其子類化以添加自定義功能,例如徽章,如下所示:
public class BadgedDrawerArrowDrawable extends DrawerArrowDrawable {
/**
* @param context used to get the configuration for the drawable from
*/
public BadgedDrawerArrowDrawable(Context context) {
super(context);
setColor(context.getResources().getColor(R.color.colorAccent));
}
@Override
public void draw(Canvas canvas) {
super.draw(canvas);
Paint paint = new Paint();
paint.setStyle(Paint.Style.FILL);
paint.setColor(Color.RED);
paint.setTextSize(60);
canvas.drawText("!", canvas.getWidth() - 60, 25, paint);
}
}
用法:
actionBarDrawerToggle.setDrawerArrowDrawable(new BadgedDrawerArrowDrawable(activity));
對我toolbar.setNavigationIcon(R.drawable.ic_camera_alt_24dp);
是我只需要調用toolbar.setNavigationIcon(R.drawable.ic_camera_alt_24dp);
在onCreate結束時,或者至少在mDrawerToggle = new ActionBarDrawerToggle...
至於v7支持庫 - 您可以創建自己的DrawerArrowDrawable表示。
mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close) {
public void onDrawerClosed(View view) {
supportInvalidateOptionsMenu();
}
public void onDrawerOpened(View drawerView) {
supportInvalidateOptionsMenu();
}
};
mDrawerToggle.setDrawerIndicatorEnabled(true);
DrawerArrowDrawable drawerArrowDrawable = new DrawerArrowDrawable(this);
drawerArrowDrawable.setAlpha(1);
drawerArrowDrawable.setSpinEnabled(false);
drawerArrowDrawable.setDirection(DrawerArrowDrawable.ARROW_DIRECTION_LEFT);
drawerArrowDrawable.setColor(Color.BLACK);
mDrawerToggle.setDrawerArrowDrawable(drawerArrowDrawable);
截至2018年1月,這是一個有效的解決方案(至少對我而言):
//setSupportActionBar(toolbar)
val toggle = ActionBarDrawerToggle(this, drawer_layout, null, R.string.navigation_drawer_open, R.string.navigation_drawer_close)
toolbar.inflateMenu(R.menu.menu_main)
toolbar.setNavigationIcon(R.drawable.ic_menu)
toolbar.setNavigationOnClickListener {
drawer_layout.openDrawer(Gravity.START)
}
toolbar.setOnMenuItemClickListener {
true
}
drawer_layout.addDrawerListener(toggle)
toggle.syncState()
nav_view.setNavigationItemSelectedListener(this)
需要注意的事項:
setSupportActionBar
ActionBarDrawerToggle
沒有引用toolbar
onCreateOptionsMenu
和onOptionsItemSelected
將無法正常運行。 toggle.syncState()
這就是我最終能夠讓我的工作方式。
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);
}
原來是
mDrawerToggle.syncState();
這終於讓一切順利。
我認為建議在onPostCreate(...)
生命周期方法中調用syncState()
。
@Override
public void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
mDrawerToggle.syncState();
}
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
final DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
final ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this,drawer,toolbar,R.string.navigation_drawer_open,R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();
toolbar.setNavigationIcon(R.drawable.ic_action_name);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.