【问题标题】:Why is the Toolbar in the middle of the screen?为什么工具栏在屏幕中间?
【发布时间】:2015-03-13 10:57:32
【问题描述】:

我正在尝试使用带有Android Developers Guide 的工具栏在棒棒糖之前的设备上启用材料主题。

我的应用主题扩展 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>

【问题讨论】:

  • 你忘记发布activity_main.xml ...没有它的内容就不可能回答这个问题
  • @Selvin 完成!发布了xml。我计划向 XML 添加更多内容。我才刚刚开始。
  • 使用 LinearLayout ... 先放 Toolbar 然后是 SwipeRefreshLayout (或者你想在 SwipeRefreshLayout 里面放 Toolbar ?)
  • @Selvin 哦,根据经验,如果我想要它在顶部,它必须是布局中的第一个视图(在父级内部)?

标签: android android-appcompat android-toolbar


【解决方案1】:

您的工具栏与 SwipeRefreshLayout 无关

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

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

【讨论】:

猜你喜欢
  • 2021-06-30
  • 2019-12-24
  • 2021-05-21
  • 2016-12-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多