【问题标题】:Android Toolbar occupies full screenAndroid Toolbar 占据全屏
【发布时间】:2015-05-17 16:25:07
【问题描述】:

这是我所有的相关代码:

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    toolbar.setTitle("sometitle");
    toolbar.inflateMenu(R.menu.menu_main);
    setSupportActionBar(toolbar);

样式:

<resources>

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="colorPrimary">@color/sunshine_blue</item>
    <item name="colorPrimaryDark">@color/sunshine_dark_blue</item>
    <item name="drawerArrowStyle">@style/DrawerArrowStyle</item>
    <item name="windowActionBar">false</item> <!-- Remove the default action bar -->
    <item name="windowNoTitle">true</item>
</style>
<style name="DrawerArrowStyle" parent="Widget.AppCompat.DrawerArrowToggle">
    <item name="spinBars">true</item>
    <item name="color">@android:color/white</item>
</style>

XML:

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

<!-- Replace the default action bar -->
<android.support.v7.widget.Toolbar
    android:id="@+id/toolbar"
    android:layout_height="?attr/actionBarSize"
    android:layout_width="match_parent"
    android:elevation="5dp"
    android:background="?attr/colorPrimary" />

<!-- The main content view -->
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools" android:id="@+id/fragment"
    android:name="com.***.***.***.MainActivityFragment"
    tools:layout="@layout/fragment_main" android:layout_width="match_parent"
    android:layout_height="match_parent" />

<!-- The navigation drawer -->
<ListView android:id="@+id/left_drawer"
    android:layout_width="240dp"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:choiceMode="singleChoice"
    android:divider="@android:color/transparent"
    android:dividerHeight="0dp"
    android:background="#111"/>

结果如下:

【问题讨论】:

  • 可能需要指定固定高度而不是使用wrap_content?
  • 如果删除 minheight 和 maxHeight 并使用特定尺寸而不是 wrap_content 会怎样?
  • 但是硬编码的尺寸是不好的做法,对吧?
  • 这是您仅有的 XML 代码吗?你不是将代码包装在另一个视图中,比如框架布局还是相对布局?
  • 不,我是@rogerthatcode .. 顺便说一句,只是为了它,我试了一下硬编码的尺寸,但它在神话中不起作用。

标签: android


【解决方案1】:

DrawerLayout 只允许 2 个孩子。来自documentation

  • 主要内容视图(上面的 FrameLayout)必须是 DrawerLayout 中的第一个孩子,因为 XML 顺序意味着 z-ordering 和抽屉必须在内容的顶部。
  • 主要 内容视图设置为匹配父视图的宽度和高度, 因为它代表了导航抽屉时的整个 UI 隐藏。

尝试将您的Toolbarfragment 包装在FrameLayout 中。

【讨论】:

  • 谢谢..虽然我已经得到了答案.. +1(我知道我们不应该制作这样的 cmets,但我认为这条规则没有意义,我们应该怎么做欣赏)
  • 这解释了问题的根本原因,而不仅仅是一个解决方案,谢谢!
【解决方案2】:

可能的解决方案是将工具栏移到 DrawerLayout 之外,并将这两个放在框架布局中。

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.v4.widget.DrawerLayout
        android:id="@+id/drawer_layout"
        android:paddingTop="?attr/actionBarSize"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <!-- The main content view -->
        <fragment
            xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:app="http://schemas.android.com/apk/res-auto"
            xmlns:tools="http://schemas.android.com/tools" android:id="@+id/fragment"
            android:name="com.***.***.***.MainActivityFragment"
            tools:layout="@layout/fragment_main" android:layout_width="match_parent"
            android:layout_height="match_parent" />

        <!-- The navigation drawer -->
        <ListView
            android:id="@+id/left_drawer"
            android:layout_width="240dp"
            android:layout_height="match_parent"
            android:layout_gravity="start"
            android:choiceMode="singleChoice"
            android:divider="@android:color/transparent"
            android:dividerHeight="0dp"
            android:background="#111"/>

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

    <!-- Replace the default action bar -->
    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_height="?attr/actionBarSize"
        android:layout_width="match_parent"
        android:elevation="5dp"
        android:background="?attr/colorPrimary" />

</FrameLayout>

【讨论】:

  • 最后一件事(对不起)我的汉堡没有旋转并动画到返回按钮,尽管我在我的样式中设置了&lt;item name="spinBars"&gt;true&lt;/item&gt;
  • 这实际上是我遵循的..我编辑了原始问题中的样式,以便您可以看到大图..
  • 如果这样toolbar将覆盖drawerlayout的一部分
  • @chinaanihchen DrawerLayout 上的属性android:paddingTop="?attr/actionBarSize" 将确保内部的片段不会被toolbar 覆盖,因为它设置的填充等于toolbar 的高度
猜你喜欢
  • 2017-03-19
  • 2019-10-05
  • 2022-07-23
  • 2017-11-30
  • 1970-01-01
  • 1970-01-01
  • 2014-05-18
  • 2017-12-29
  • 1970-01-01
相关资源
最近更新 更多