应用程序图标在android棒棒糖及以上版本的Actionbar上不可见
我刚刚从api 19 Kitkat迁移到api 21棒棒糖。现在我发现应用程序图标不在actionbar上。我觉得我的应用看起来有点不一样。那么有没有办法显示应用程序图标。试试这个:-应用程序图标在android棒棒糖及以上版本的Actionbar上不可见,android,android-actionbar,icons,Android,Android Actionbar,Icons,我刚刚从api 19 Kitkat迁移到api 21棒棒糖。现在我发现应用程序图标不在actionbar上。我觉得我的应用看起来有点不一样。那么有没有办法显示应用程序图标。试试这个:- getSupportActionBar.setDisplayHomeAsUpEnabled(true); 在Material主题(以及基于它的AppCompat版本21)中,操作栏遵循并使用: 标题和副标题。标题应该是工具栏在导航层次结构中的当前位置以及其中包含的内容的路标。副标题(如有)应指示有关当前内容的
getSupportActionBar.setDisplayHomeAsUpEnabled(true);
在Material主题(以及基于它的AppCompat版本21)中,操作栏遵循并使用:
- 标题和副标题。标题应该是工具栏在导航层次结构中的当前位置以及其中包含的内容的路标。副标题(如有)应指示有关当前内容的任何扩展信息<强>如果应用程序使用徽标图像,则应强烈考虑省略标题和字幕。
但是,如果你想要一个应用程序图标,这是正确的方法。我还想用棒棒糖+显示我的应用程序图标,下面是我使用的
mActionBar.setDisplayShowHomeEnabled(true);
mActionBar.setIcon(Drawable); // Or drawable resource id.
来源:这对我很有效-
getSupportActionBar().setTitle("Title");
getSupportActionBar().setSubtitle("Subtitle");
getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_HOME |
ActionBar.DISPLAY_SHOW_TITLE | ActionBar.DISPLAY_HOME_AS_UP | ActionBar.DISPLAY_USE_LOGO); // Customize if you need to
getSupportActionBar().setIcon(R.drawable.iconhere); // or setLogo
设置显示选项非常重要,因为没有它,setIcon/setLogo似乎无法工作。原答覆-
这个代码在我的设备上工作。如果您还有其他问题,请告诉我。我就是这样做的:
package com.your.package;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getSupportActionBar().setDisplayShowHomeEnabled(true);
getSupportActionBar().setIcon(R.drawable.your_icon);
}
}
这将为您提供以下信息:
您必须在应用程序中使用工具栏,如下所示 styles.xml
<!-- Base application theme. -->
<style name="AppTheme" parent="AppTheme.Base">
<!-- Customize your theme here. -->
</style>
<style name="AppTheme.Base" parent="Theme.AppCompat.Light.NoActionBar">
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
</style>
<resources>
<style name="AppTheme" parent="AppTheme.Base">
<item name="android:windowContentTransitions">true</item>
<item name="android:windowAllowEnterTransitionOverlap">true</item>
<item name="android:windowAllowReturnTransitionOverlap">true</item>
<item name="android:windowSharedElementEnterTransition">@android:transition/move</item>
<item name="android:windowSharedElementExitTransition">@android:transition/move</item>
<item name="android:textColorPrimary">@color/textColorPrimary</item>
<item name="android:windowBackground">@color/windowBackground</item>
<item name="android:navigationBarColor">@color/navigationBarColor</item>
</style>
</resources>
我试过了,但它显示了一个后箭头图标,我需要应用程序图标。这可以帮助:如何以及在何处使用setlogo()方法?如果您愿意,您可以在
onCreate()
中的getSupportActionBar()调用它-[ActionBar-显示选项][1][1]:整个上午都在搜索解决方案。这个有效。谢谢
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="false"
android:focusable="false"
android:focusableInTouchMode="false">
<RelativeLayout xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/windowBackground">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/colorPrimary"
android:minHeight="?attr/actionBarSize"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
</android.support.v7.widget.Toolbar>
<FrameLayout
android:id="@+id/flContent"
android:visibility="gone"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/toolbar">
</FrameLayout>
</RelativeLayout>
</android.support.v4.widget.DrawerLayout>
toolbar = (Toolbar) findViewById(R.id.toolbar);
if (toolbar != null) {
setSupportActionBar(toolbar);
toolbar.setNavigationIcon(R.drawable.icn_menu);
//toolbar.setTitle("Title");
setTitle("Title");
//toolbar.setSubtitle("Subtitle");
//toolbar.setLogo(R.drawable.ic_launcher);
}