如何让工具栏不与Android中的其他内容重叠?

时间:2015-10-29 18:42:47

标签: android

我正在尝试使用工具栏开发一个活动(该示例或多或少取自教程),但工具栏总是重叠其他内容的一部分。 这是一个截图:

enter image description here

蓝色工具栏与其他一些内容重叠。我试图在SO上搜索类似的问题,但只找到了一些无关的东西。我还尝试更改某些元素的顺序并替换wrap_content< - > match_parent最多会使布局恶化。

我确信我错过了一些非常基本的东西,但我看不出是什么。

activity_main.xml代码:

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
    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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context=".MainActivity" >

    <android.support.design.widget.AppBarLayout
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.v7.widget.Toolbar android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"/>

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

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

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

content_main.xml代码:

<?xml version="1.0" encoding="utf-8"?>
  <GridLayout
        xmlns:android="http://schemas.android.com/apk/res/android"

        android:layout_width="wrap_content"
        android:layout_height="match_parent"

        android:useDefaultMargins="true"
        android:alignmentMode="alignBounds"
        android:columnOrderPreserved="false"

        android:columnCount="4"
        >

    <TextView
            android:text="@string/MainTitle"
            android:textSize="32dip"
            android:layout_columnSpan="4"
            android:layout_gravity="center_horizontal"
            android:id="@+id/textView1"
            />

    <TextView
            android:text="You can configure email in just a few steps:"
            android:textSize="16dip"

            android:layout_columnSpan="4"
            android:layout_gravity="left"
            />

    <TextView
            android:text="Email address:"

            android:layout_gravity="right"
            />

    <EditText
            android:ems="10"
            />

    <TextView
            android:text="Password:"

            android:layout_column="0"
            android:layout_gravity="right"
            />

    <EditText
            android:ems="8"
            />

    <Space
            android:layout_row="4"
            android:layout_column="0"
            android:layout_columnSpan="3"
            android:layout_gravity="fill"
            />

    <Button
            android:text="Next"
            android:id="@+id/imageButton1"
            android:layout_row="5"
            android:layout_column="3"
            />
</GridLayout>

6 个答案:

答案 0 :(得分:71)

将此var oldX:Number = c.x; var oldY:Number = c.y; c.x = ExtraMath.PointTowards(oldX, oldY, stage.mouseX, stage.mouseY, 5); c.y = ExtraMath.PointTowards(oldX, oldY, stage.mouseX, stage.mouseY, 5, "y"); 替换为:

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

答案 1 :(得分:13)

将此添加到工具栏下方的视图

app:layout_behavior="@string/appbar_scrolling_view_behavior"

答案 2 :(得分:5)

在你的布局中尝试这个:

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
    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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context=".MainActivity" >

    <android.support.design.widget.AppBarLayout
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.v7.widget.Toolbar android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"/>

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

    <GridLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        android:layout_width="match_parent"
        android:layout_height="match_parent"

        android:useDefaultMargins="true"
        android:alignmentMode="alignBounds"
        android:columnOrderPreserved="false"

        android:columnCount="4"
        >

        <TextView
            android:text="MainTitle"
            android:textSize="32dip"
            android:layout_columnSpan="4"
            android:layout_gravity="center_horizontal"
            android:id="@+id/textView1"
            />

        <TextView
            android:text="You can configure email in just a few steps:"
            android:textSize="16dip"

            android:layout_columnSpan="4"
            android:layout_gravity="left"
            />

        <TextView
            android:text="Email address:"

            android:layout_gravity="right"
            />

        <EditText
            android:ems="10"
            />

        <TextView
            android:text="Password:"

            android:layout_column="0"
            android:layout_gravity="right"
            />

        <EditText
            android:ems="8"
            />

        <Space
            android:layout_row="4"
            android:layout_column="0"
            android:layout_columnSpan="3"
            android:layout_gravity="fill"
            />

        <Button
            android:text="Next"
            android:id="@+id/imageButton1"
            android:layout_row="5"
            android:layout_column="3"
            />
    </GridLayout>

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

答案 3 :(得分:4)

只是一个简单的改变就可以了。

我以前的代码:

activity_device_scan.xml

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

activity_device_scan.xml:

    <include layout="@layout/content_device_scan"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"/>

答案 4 :(得分:0)

如果 “ @ string / appbar_scrolling_view_behavior”

没有工作尝试直接提及,例如

app:layout_behavior =“ android.support.design.widget.AppBarLayout $ ScrollingViewBehavior”

答案 5 :(得分:0)

我将activity_main的CoordinatorLayout更改为垂直线性布局,并且可以正常工作。我让Android Studio为我转换了视图类型,起初它破坏了我对content_main的包含。我在转换过程中恢复了包含标注。