actionbar中的Android配置文件图像

actionbar中的Android配置文件图像,android,android-actionbar,Android,Android Actionbar,如何将配置文件图像放入默认操作栏中 目前,我的应用程序就像下面的图片: 在上图中,第一个Actionbar是默认的,第二个(带有图像和配置文件的名称)是我创建的 我想使用默认的ActionBar,其中包含图像,如下图所示: 也许我需要自己创建一个自定义的Actionbar,但是如果我有办法使用默认的Actionbar,我会试试。你可以使用自定义的Actionbar setTitle("title"); getActionBar().setIcon(R.drawable.profile_ico

如何将配置文件图像放入默认操作栏中

目前,我的应用程序就像下面的图片:

在上图中,第一个Actionbar是默认的,第二个(带有图像和配置文件的名称)是我创建的

我想使用默认的ActionBar,其中包含图像,如下图所示:


也许我需要自己创建一个自定义的Actionbar,但是如果我有办法使用默认的Actionbar,我会试试。

你可以使用自定义的Actionbar

setTitle("title");
getActionBar().setIcon(R.drawable.profile_icon);
actionbar.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/profile_icon" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="APP_Name" />
</LinearLayout>
Objects.requireNonNull(getSupportActionBar()).setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
getSupportActionBar().setCustomView(R.layout.actionbar);

您可以使用以下代码在操作栏上设置图标

setTitle("title");
getActionBar().setIcon(R.drawable.profile_icon);

如果您有固定的徽标,则在
manifest
文件中添加以下代码

针对单个活动

<activity android:name=".MyActivity" 
       android:icon="@drawable/profile_icon" 
       android:label="title" />  

适用于所有活动

<application
    android:logo="@drawable/profile_icon">

    ...

</application>

...

您可以使用折叠工具栏,因为它将在折叠时折叠并显示为单个操作栏。当你被拖下来的时候,你会得到里面所有的东西

看,这可能会对你有帮助。正如我所想。。。我必须创建自己的动作栏。Android Studio的文档告诉我们,在第21版之后,在操作栏上插入名称和图标不是一种最佳做法,只能插入一个或另一个,永远不能同时插入两个。谢谢大家。我们怎样才能为科特林做到这一点?