显示的 AppCompat 工具栏无效

Invalid displayed AppCompat toolbar

我试图在我的应用程序中添加 AppCompat 工具栏,但我无法以正确的方式显示它。我按照本教程中的步骤进行操作:http://www.android4devs.com/2014/12/how-to-make-material-design-app.html 但工具栏与 ListView 重叠并且部分分开。我不知道如何描述它,所以这是我的应用程序的屏幕:

下面我附上工具栏代码和我的视图。你能帮忙修一下工具栏吗?

工具栏:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/toolbar_primary"
>

</android.support.v7.widget.Toolbar>

Activity 视图:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:paddingTop="10dp"
android:paddingBottom="10dp"
tools:context="bak.grzegorz.pl.smm.Activities.MessageList">

<include
    android:id="@+id/toolbar"
    layout="@layout/toolbar" />

<ListView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@android:id/list"
    android:layout_centerVertical="true"
    android:layout_centerHorizontal="true"
    />

</RelativeLayout>

它重叠是因为您使用的是 RelativeLayout,请在列表视图中使用 "layout_below" 标记或使用 LinearLayout。您还在主 RelativeLayout 中添加了填充,因此它会修改工具栏的位置。

使用 NoActionBar 主题删除默认的旧操作栏

style.xml

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

AndroidMainifest.xml

<application
    android:theme="@style/AppTheme"
    ... >

删除 RelativeLayout 上的填充并在 ListView 上添加 layout_below 标志

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="bak.grzegorz.pl.smm.Activities.MessageList">

    <include
        android:id="@+id/toolbar"
        layout="@layout/toolbar" />

    <ListView
        android:layout_below="@+id/toolbar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@android:id/list"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true"
        />

</RelativeLayout>

在您的 MainActivity 中将工具栏设置为默认操作栏

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Toolbar toolbar = (Toolbar) findViewById(R.id.tool_bar);
    setSupportActionBar(toolbar);

}