为什么工具栏在屏幕中间?

Why is the Toolbar in the middle of the screen?

我正在尝试通过 Android Developers Guide 使用工具栏,以便在棒棒糖之前的设备上启用 material 主题。

我的应用主题扩展 Theme.AppCompat.Light.NoActionBar 如下:

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

windowActionBar 设置为 false,如下所示:

<style name="AppTheme" parent="AppBaseTheme">
        <item name="colorPrimary">#EEEEEE</item>
        <item name="windowActionBar">false</item>
</style>  

我的 MainActivity 看起来像这样:

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

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

但工具栏出现在屏幕中间而不是顶部。我究竟做错了什么?

activity_main.xml:

<android.support.v4.widget.SwipeRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/swipe_container"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:minHeight="40dp"
        android:background="#EEEEEE" />

</android.support.v4.widget.SwipeRefreshLayout>

您的工具栏与 SwipeRefreshLayout 无关

您必须在 Toolbar 和 SwipeRefreshLayout 周围使用 LinearLayout(垂直)。

<LinearLayout orientation="vertical">
<Toolbar />
<SwipeRefreshLayout>
</SwipeRefreshLayout>
</LinearLayout>