【问题标题】:App icon is not visible on Actionbar in android Lollipop and above应用程序图标在 android Lollipop 及更高版本的操作栏上不可见
【发布时间】:2015-02-16 19:55:06
【问题描述】:

我只是从 api 19 Kitkat 迁移到 api 21 Lollipop。现在我发现操作栏上没有应用程序图标。我觉得我的应用看起来有点不同。那么有没有办法显示应用程序图标。

【问题讨论】:

标签: android android-actionbar icons


【解决方案1】:

在 Material 主题(以及基于它的 AppCompat 版本 21)中,Action Bar 遵循material design guidelines 并使用Toolbar

  • 标题和副标题。标题应该是工具栏在导航层次结构中的当前位置以及其中包含的内容的路标。副标题(如果存在)应指示有关当前内容的任何扩展信息。 如果应用使用徽标图像,则应强烈考虑省略标题和副标题。

在现代 Android 用户界面中,开发人员应该更多地依赖于工具栏视觉上独特的配色方案,而不是应用程序图标。 不鼓励在 API 21 及更高版本的设备上使用应用程序图标加标题作为标准布局。

但是,如果你想要一个应用程序图标,setLogo() 是正确的方法。

【讨论】:

  • 如何以及在何处使用 setlogo() 方法?
  • 如果您愿意,可以在onCreate() 中的getSupportActionBar() 上调用它
【解决方案2】:

我还想在 Lollipop+ 中显示我很棒的应用程序图标,所以这就是我使用的。

mActionBar.setDisplayShowHomeEnabled(true);
mActionBar.setIcon(Drawable); // Or drawable resource id.

来源:ActionBar#setDisplayShowHomeEnabled(boolean)

【讨论】:

    【解决方案3】:

    这对我有用 -

    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
    

    setDisplayOptions 很重要,因为没有它 setIcon/setLogo 似乎无法工作。原始答案-App compat actionbar v21 app icon is not showing

    此代码在我的设备上运行。如果您有任何其他问题,请告诉我。

    【讨论】:

    • 整个上午都在寻找解决方案。这个有效。谢谢!
    【解决方案4】:

    我就是这样做的:

    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);
        }
    }
    

    这会给你这个:

    【讨论】:

      【解决方案5】:

      您必须在您的应用中使用工具栏,如下所示。

      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>
      

      v21\styles.xml

      <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>
      

      在你的布局中使用这样的工具栏:

      <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>
      

      在您的 Java 文件中为设置图标编写以下代码:

      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);
              }
      

      【讨论】:

        【解决方案6】:

        试试这个:-

        getSupportActionBar.setDisplayHomeAsUpEnabled(true);
        

        【讨论】:

        • 我试过了,但它显示一个后退箭头图标,我需要应用程序图标。
        • 这可以帮助:link
        猜你喜欢
        • 2014-08-31
        • 2016-12-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-03-04
        相关资源
        最近更新 更多