java 更改左侧的 ToolBar 默认图标
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34292674/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Change ToolBar default icon on the left
提问by cubeb
I am using a ToolBar at the top at present and want to replace the default back button with a home button. But when I add item, it always adds to the right. I don't see any layout_gravity options to choose either. Is there a way to do this?
我目前正在使用顶部的 ToolBar,并想用主页按钮替换默认的后退按钮。但是当我添加项目时,它总是添加到右边。我也没有看到任何 layout_gravity 选项可供选择。有没有办法做到这一点?
MainActivity
主要活动
Toolbar toolbar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_delete);
toolbar = (Toolbar)findViewById(R.id.app_bar);
setSupportActionBar(toolbar);
getSupportActionBar().setTitle(" Testing");
getSupportActionBar().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.home_icon_empty, menu);
return true;
}
home_icon_empty.xml
home_icon_empty.xml
<item
android:id="@+id/homepage"
android:orderInCategory="100"
android:title="HOME"
android:icon="@drawable/home_icon"
app:showAsAction="always"
/>
<item
android:id="@+id/homepage1"
android:orderInCategory="200"
android:title="HOME"
android:icon="@drawable/home_icon"
app:showAsAction="always"
/>
activity_main.xml
活动_main.xml
<include
android:id="@+id/app_bar"
layout="@layout/app_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
app_bar.xml
app_bar.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/colorPrimary">
</android.support.v7.widget.Toolbar>
Current Toolbar output:
当前工具栏输出:
Expected Toolbar output:
预期的工具栏输出:
Image after new edit:
新编辑后的图像:
回答by Deividi Cavarzan
Set as NavigationIcon:
设置为导航图标:
Drawable drawable = ContextCompat.getDrawable(context, R.drawable.your_drawable)
toolbar.setNavigationIcon(drawable);
setSupportActionBar(toolbar);
回答by RexSplode
Try this
试试这个
toolbar.setNavigationIcon(R.drawable.your_icon);
Use it before setting support action bar. And don't call this
在设置支持操作栏之前使用它。不要叫这个
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
Or at least do
或者至少做
getSupportActionBar().setDisplayHomeAsUpEnabled(false);
Try it out. Some of this will work.
试试看。其中一些会起作用。
回答by Hitesh Sahu
Add your drawable asset with size as per Material design guideand call below method in Activity onCreate
根据材料设计指南添加具有大小的可绘制资产, 并在活动中调用以下方法onCreate
private void setUpToolBar() {
setSupportActionBar(toolbar);
//Set Home screen icon
getSupportActionBar().setHomeAsUpIndicator(R.drawable.home_screen_icon);
//Enable Home icon
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
//Toolbar title
getSupportActionBar().setTitle(getString(R.string.app_name));
//Set toolbar as action bar
setSupportActionBar(toolbar);
}
回答by Z3R0
You can also:
你也可以:
mToolbar.setLogo( yourLogoID );
or you can:
或者你可以:
mToolbar.setBackground( ContextCompat.getDrawable( yourContext, urBackgroundID );
where
在哪里
mToolbar = yourToolbar;
mToolbar = findViewById(R.id.your_toolbar);
// or, if your toolbar get setted by a superclass
mToolbar = (Toolbar) getSupportActionBar();
your style should be: Blablabla.NoActionBar then in xml layout include your toolbar
你的风格应该是: Blablabla.NoActionBar 然后在 xml 布局中包括你的工具栏
If you are using a Drawer to show left Menu when Left Swiped or Home Button Clicked then you should use one of above methods (If you don't want to superclass all DrawerLayout lul)
如果您使用 Drawer 在向左滑动或单击主页按钮时显示左侧菜单,那么您应该使用上述方法之一(如果您不想超类所有 DrawerLayout lul)
bb
bb