如何将个人资料图像放在默认的ActionBar中?
一段时间以来,我的应用程序就像下面的图像:
在上面的图像中,第一个操作栏是默认的,第二个操作栏(带有图像和配置文件的名称)是我创建的。
我想将默认的ActionBar与内部图像一起使用,如下面的图像所示:
也许我需要自己创建一个自定义Actionbar,但是如果可以使用默认的ActionBar,我会尝试的。
您可以使用折叠工具栏,因为它会折叠并在折叠时显示为单个操作栏。向下拖动时,您将获得其中的所有内容。
您可以使用自定义操作栏
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>
在Activity.java
中添加以下行
Objects.requireNonNull(getSupportActionBar()).setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
getSupportActionBar().setCustomView(R.layout.actionbar);
OR
您可以使用以下代码在操作栏上设置图标
setTitle("title");
getActionBar().setIcon(R.drawable.profile_icon);
OR
如果您具有不变的徽标,则在manifest
文件中添加以下代码
用于单项活动
<activity android:name=".MyActivity"
android:icon="@drawable/profile_icon"
android:label="title" />
对于所有活动
<application
android:logo="@drawable/profile_icon">
...
</application>