【问题标题】:Custom image instead of logo in ActionBar自定义图像而不是 ActionBar 中的徽标
【发布时间】:2016-11-07 07:33:44
【问题描述】:

如何将 ActionBar 的 logo 图标设置为自定义图片?类似于它在 Whatsapp 上的工作方式?我试图实现,但它看起来像这样..

如何删除空格?

我是这样用的

getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayUseLogoEnabled(true);
getSupportActionBar().setIcon(R.drawable.dummy_user_profile_pic);

在清单中

<application
        android:logo="@drawable/dummy_user_profile_pic"

我做错了什么?请指导我。非常感谢

【问题讨论】:

  • 可以分享dummy_user_profile_pic的内容吗?
  • 那是png图片
  • 我认为问题出在那个图像上......我已经改变了正确的空间去......但是在箭头和图像之间,空间正在显示......

标签: android android-actionbar android-homebutton


【解决方案1】:

使用

setLogo(R.drawable.dummy_user_profile_pic);

在您的代码中,而不是

setIcon(R.drawable.dummy_user_profile_pic);

它应该可以正常工作。

【讨论】:

  • 是的,我第一次尝试使用 SetLogo,但它不起作用..然后我改为 SetIcon,这也不起作用..
  • 尝试使用:'getActionBar.setIcon(R.drawable.dummy_user_profile_pic);'在您的代码中。
  • android:icon="@drawable/dummy_user_profile_pic" // 对不起三个 cmets .. 我刚开始用这个 .. 我越来越熟悉这个了
  • 在应用程序下的清单文件中,我们不能将它作为图标,图标是指应用程序启动器图标..我试过 getActionBar 它不工作仍然有空间
  • stackoverflow.com/questions/31788678/… 我参考了这个链接并实现了但这里箭头和图像之间也有空格..在whatsapp中没有空格...
【解决方案2】:

我提到了这个link,我可以实现它。但是 home 和 image 之间的空间是默认空间来自 App-compat Support Library Toolbar 而在 Whatsapp 中没有空间。我认为他们没有使用支持库工具栏。提供动作栏的标题时,您可以看到默认空间。

【讨论】:

    【解决方案3】:
       With toolbar you can easily customize actionbar as shown below 
    
        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="@color/black"
            app:popupTheme="@style/AppTheme.PopupOverlay">
    
            <ImageView
                android:layout_width="40dip"
                android:layout_height="40dip"
                android:layout_gravity="center_vertical"
                android:layout_marginRight="10dip"
                android:src="@drawable/logo" />
    
          <TextView android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:text="Sample"
                android:layout_weight="1"
                android:id="@+id/title_textview"
                android:textColor="@android:color/white"
                android:gravity="center"/>
    
        </android.support.v7.widget.Toolbar>
    

    注意:使用此方法时不要使用getSupportactionbar.setTitle() 方法 只需在工具栏内设置textview的文本

    【讨论】:

    • 在我提到的那个链接中使用了同样的方法...检查工具栏小部件下的包含布局...
    猜你喜欢
    • 2013-10-10
    • 2012-02-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多