问题描述
我试图使用定义的支持工具栏自定义图标,但显示的唯一的图标是一个左箭头......我试着将它设置在布局和编程,但结果是一样的。
下面是我的活动
公共类MainActivity扩展ActionBarActivity {
@覆盖
保护无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.activity_main);
工具条工具栏=(栏)findViewById(R.id.my_toolbar);
toolbar.setNavigationIcon(R.drawable.ic_launcher);
toolbar.setTitle();
setSupportActionBar(工具栏);
}
和我的工具栏布局
< android.support.v7.widget.Toolbar
的xmlns:机器人=http://schemas.android.com/apk/res/android
的xmlns:程序=http://schemas.android.com/apk/res-auto
机器人:ID =@ + ID / my_toolbar
机器人:layout_height =WRAP_CONTENT
机器人:layout_width =match_parent
应用程序:navigationIcon =@可绘制/ ic_action_bar
机器人:=了minHeight@扪/ action_bar_size
机器人:ATTR / colorPrimary后台=
应用程序:主题=@风格/ ThemeOverlay.AppCompat.Dark.ActionBar
应用程序:popupTheme =@风格/ ThemeOverlay.AppCompat.Light
/>
刚刚尝试过自己和这个问题似乎是,你必须调用 setNavigationIcon
在 setSupportActionBar(工具栏)
。否则,它只会显示箭头你所描述。
因此,要解决这个问题,只是改变了code是这样的:
// ...
工具条工具栏=(栏)findViewById(R.id.my_toolbar);
setSupportActionBar(工具栏);
toolbar.setNavigationIcon(R.drawable.ic_launcher);
toolbar.setTitle();
注意:也是一样的设置标题,contentDescription等。我不知道这是一个错误,或者如果它的目的,但它绝对是有点儿奇怪。
I'm trying to use define a custom icon in the support Toolbar but the only icon shown is a left arrow... I tried to set it in the layout and programmatically but the result is the same.
Here is my Activity
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.my_toolbar);
toolbar.setNavigationIcon(R.drawable.ic_launcher);
toolbar.setTitle("");
setSupportActionBar(toolbar);
}
And my toolbar layout
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/my_toolbar"
android:layout_height="wrap_content"
android:layout_width="match_parent"
app:navigationIcon="@drawable/ic_action_bar"
android:minHeight="@dimen/action_bar_size"
android:background="?attr/colorPrimary"
app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
/>
Just tried it myself and the issue seems to be that you have to call setNavigationIcon
after setSupportActionBar(toolbar)
. Otherwise it'll only show the arrow as you've described.
So to fix this issue just change the code like this:
//...
Toolbar toolbar = (Toolbar) findViewById(R.id.my_toolbar);
setSupportActionBar(toolbar);
toolbar.setNavigationIcon(R.drawable.ic_launcher);
toolbar.setTitle("");
Note: Same goes for setting the title, contentDescription etc. I don't know if this a bug or if it is intended, but it's definitely kinda strange.
这篇关于在Android的工具栏自定义图标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!