Android 使用 AppCompat-v7 21 在操作栏/工具栏中显示图标
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26440279/
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
show icon in actionbar/toolbar with AppCompat-v7 21
提问by ligi
I tried these - but still do not see the icon like before:
我尝试了这些 - 但仍然看不到像以前那样的图标:
getSupportActionBar().setLogo(R.drawable.ic_launcher);
getSupportActionBar().setDisplayUseLogoEnabled(true);
getSupportActionBar().setIcon(R.drawable.ic_launcher);
It seems to work when I use custom toolbar - but that would force me to touch all layouts - is there a better way to do so?
当我使用自定义工具栏时它似乎工作 - 但这会迫使我触摸所有布局 - 有没有更好的方法来做到这一点?
回答by nadavfima
getSupportActionBar().setDisplayShowHomeEnabled(true);
along with
随着
getSupportActionBar().setIcon(R.drawable.ic_launcher);
回答by LordRaydenMK
In modern Android UIs developers should lean more on a visually distinct color scheme for toolbars than on their application icon. The use of application icon plus title as a standard layout is discouraged on API 21 devices and newer.
在现代 Android UI 中,开发人员应该更多地依赖于工具栏的视觉上不同的配色方案,而不是他们的应用程序图标。在 API 21 和更新版本的设备上不鼓励使用应用程序图标和标题作为标准布局。
If you disagree you can try with:
如果您不同意,您可以尝试:
To create the toolbar in XML:
要在 XML 中创建工具栏:
<android.support.v7.widget.Toolbar
android:id="@+id/my_awesome_toolbar"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:minHeight="?attr/actionBarSize"
android:background="?attr/colorPrimary" />
In your activity:
在您的活动中:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.my_layout);
Toolbar toolbar = (Toolbar) findViewById(R.id.my_awesome_toolbar);
setSupportActionBar(toolbar);
}
Use the setLogo()method to set the icon. Code source.
回答by Micha? K
This worked for me:
这对我有用:
getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);
getSupportActionBar().setDisplayUseLogoEnabled(true);
getSupportActionBar().setLogo(R.drawable.ic_logo);
getSupportActionBar().setDisplayShowTitleEnabled(false); //optional
as well as:
也:
getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);
getSupportActionBar().setIcon(R.drawable.ic_logo); //also displays wide logo
getSupportActionBar().setDisplayShowTitleEnabled(false); //optional
回答by Pila
simplest thing to do; just add:
最简单的事情;只需添加:
app:navigationIcon="@drawable/ic_action_navigation_menu">
to the <android.support.v7.widget.Toolbar
tag
到<android.support.v7.widget.Toolbar
标签
where @drawable/ic_action_navigation_menu
is the name of icon
@drawable/ic_action_navigation_menu
图标名称在哪里
回答by Vintesh
A better way for setting multiple options:
设置多个选项的更好方法:
setIcon/setLogo
method will only work if you have set DisplayOptionsTry this -
setIcon/setLogo
方法仅在您设置DisplayOptions时才有效试试这个 -
actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_HOME | ActionBar.DISPLAY_SHOW_TITLE);
actionBar.setIcon(R.drawable.ic_launcher);
You can also set options for displaying LOGO(just add constant ActionBar.DISPLAY_USE_LOGO
). More information - displayOptions
您还可以设置显示 LOGO 的选项(只需添加常量ActionBar.DISPLAY_USE_LOGO
)。更多信息 - displayOptions
回答by Chris Banes
Try using:
尝试使用:
ActionBar ab = getSupportActionBar();
ab.setHomeButtonEnabled(true);
ab.setDisplayUseLogoEnabled(true);
ab.setLogo(R.drawable.ic_launcher);
回答by Peter Ivanov
For Actionbar:
对于操作栏:
getActionBar().setDisplayHomeAsUpEnabled(true);
getActionBar().setHomeAsUpIndicator(R.drawable.ic_action_back);
For Toolbar:
对于工具栏:
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeAsUpIndicator(R.drawable.ic_action_back);
回答by smoothumut
if you want to set the home or back icon (not logo or static icon) so you can use
如果要设置主页或后退图标(不是徽标或静态图标),则可以使用
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeAsUpIndicator( getResources().getDrawable(R.drawable.home) );
回答by Skitty
getSupportActionBar().setDisplayShowHomeEnabled(true);
getSupportActionBar().setIcon(R.drawable.ic_launcher);
ORmake a XML layout call the tool_bar.xml
或使 XML 布局调用tool_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"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:background="@color/colorPrimary"
android:theme="@style/ThemeOverlay.AppCompat.Dark"
android:elevation="4dp">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@color/colorPrimary"
android:src="@drawable/ic_action_search"/>
</RelativeLayout>
</android.support.v7.widget.Toolbar>
Now in you main activity add this line
现在在你的主要活动中添加这一行
<include
android:id="@+id/tool_bar"
layout="@layout/tool_bar" />
回答by Malek Hijazi
If you dont want to set your toolbar as action bar using setSupportActionBar, you can add a logo next to your navigation icon (if you have a back button for example) like this:
如果您不想使用setSupportActionBar将工具栏设置为操作栏,您可以在导航图标旁边添加一个徽标(例如,如果您有后退按钮),如下所示:
toolbar.setLogo();
or in xml
或在 xml
<android.support.v7.widget.Toolbar
....
android:logo="@drawable/logo"
app:logo="@drawable/logo"/>
And even if you have a title set on the Toolbar, the the title will still show.
即使您在工具栏上设置了标题,标题仍会显示。
Ex: The green check in the image below is the logo
例如:下图中的绿色对勾是徽标