如何创建自定义 ActionBar?

How can I create my custom ActionBar?

我是 Android 的新手,我想为我的应用程序创建一个自定义操作栏,如下图所示:

我该如何创建它?在圆圈中,我想插入我的图像而不是 "welcome",我想写我的用户名。 我能为此做些什么? 感谢任何帮助。

编辑:

我的布局主题是 AppTheme,它有一个我不想要的操作栏,如何删除它以插入自定义操作栏?

在 Manifest 文件的 activity 标签中添加以下内容

android:label="Welcome"

您需要在活动 xml 文件中添加一个工具栏和一个浮动操作按钮 像这样将工具栏添加为 AppBar 的子项

<android.support.design.widget.AppBarLayout
            .....>

            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                ...../>

        </android.support.design.widget.AppBarLayout>

对于浮动操作按钮,在 activity xml 文件中添加以下内容

 <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="16dp"
        app:layout_anchor="@id/toolbar"
        app:layout_anchorGravity="bottom|right|end"
        <item name="android:src">@drawable/name_of_image</item>/>

在搜索我的问题后,我找到了我的回应: 我创建了一个带有线性布局的 xml 文件,并在其标签中添加了我在下面插入的代码。

<FrameLayout
    android:layout_width="match_parent"
    android:layout_height="120dp"
    android:layout_gravity="right">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:minHeight="?attr/actionBarSize"
        android:layout_width="match_parent"
        android:layout_height="80dp"
        android:background="@color/blue"
        >


    </android.support.v7.widget.Toolbar>
    <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:id="@+id/img1"
                    android:src="@drawable/books2"
                    android:background="@drawable/icon"
                    android:layout_alignParentTop="true"
                    android:layout_alignRight="@+id/toolbar"

        android:layout_gravity="bottom|right" />


</FrameLayout>

对于Imageview的形状,我使用这个代码:

<?xml version="1.0" encoding="utf-8"?>

<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">

<gradient
    android:angle="90"
    android:startColor="@color/blue"
    android:centerColor="@color/blue"
    android:endColor="@color/blue"
    android:type="linear"/>

<corners
    android:radius="70dp"
    />
<size
    android:height="40dp"
    android:width="40dp"
    />

我将其包含在我的 activity 布局 XML 文件中。

<include layout="@layout/titlebar"/>

对于这部分问题: 编辑: 我的布局主题是 AppTheme,它有一个我不想要的操作栏,如何删除它以插入自定义操作栏?

我将此代码添加到 style.xml 文件中:

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
</style>

在标签内的 AndroidManifest.xml 文件中,我添加了这个:

android:theme="@style/AppTheme"

希望对你有用。