Android 工具栏与LinearLayout的GridView重叠

Android 工具栏与LinearLayout的GridView重叠,android,xml,Android,Xml,工具栏与线性布局的GridView重叠,因此第一行中80%以上的内容被其上方的工具栏隐藏 这是在我在activity\u main中添加floatingAction按钮后发生的。我试图在LinearLayout中包含工具栏,但它给了我一个强制转换异常。如果可以将工具栏包括在LinearLayout中,我可以接受。另外,我尝试不使用marginTop或paddingTop静态处理它 下面是它现在的样子: 以下是xml文件: 活动\u main.xml <?xml version="1.0"

工具栏与线性布局的GridView重叠,因此第一行中80%以上的内容被其上方的工具栏隐藏

这是在我在
activity\u main
中添加
floatingAction按钮后发生的。我试图在LinearLayout中包含工具栏,但它给了我一个强制转换异常。如果可以将工具栏包括在LinearLayout中,我可以接受。另外,我尝试不使用
marginTop
paddingTop
静态处理它

下面是它现在的样子:

以下是xml文件:

活动\u 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="edu.ahduni.seas.gyapak.MainActivity">

<android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    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"
        app:popupTheme="@style/AppTheme.PopupOverlay" />

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

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

<android.support.design.widget.FloatingActionButton
    android:id="@+id/fab"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom|end"
    android:layout_margin="@dimen/fab_margin"
    android:src="@android:drawable/ic_dialog_email" />

</android.support.design.widget.CoordinatorLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="@+id/textlayout"
tools:context="edu.ahduni.seas.gyapak.MainActivity">

    <GridView xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/grid"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:columnWidth="90dp"
        android:numColumns="auto_fit"
        android:verticalSpacing="10dp"
        android:horizontalSpacing="10dp"
        android:stretchMode="columnWidth"
        android:gravity="center"
        android:padding="10dp" />
</LinearLayout>

content\u 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="edu.ahduni.seas.gyapak.MainActivity">

<android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    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"
        app:popupTheme="@style/AppTheme.PopupOverlay" />

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

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

<android.support.design.widget.FloatingActionButton
    android:id="@+id/fab"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom|end"
    android:layout_margin="@dimen/fab_margin"
    android:src="@android:drawable/ic_dialog_email" />

</android.support.design.widget.CoordinatorLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="@+id/textlayout"
tools:context="edu.ahduni.seas.gyapak.MainActivity">

    <GridView xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/grid"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:columnWidth="90dp"
        android:numColumns="auto_fit"
        android:verticalSpacing="10dp"
        android:horizontalSpacing="10dp"
        android:stretchMode="columnWidth"
        android:gravity="center"
        android:padding="10dp" />
</LinearLayout>

解决方案

显然,有很多解决方案:(答案和评论讨论值得称赞)

1.
content\u main.xml
的线性布局中包括
android:layout\u marginTop=“?attr/actionBarSize”
android:paddingTop=“anydpthattououou进一步想要调整”

2.包括
app:layout\u behavior=“@string/appbar\u scrolling\u view\u behavior”
content\u main.xml
Credit:@Burhanuddin Rashid


3.这是一个很长的问题,请检查@AbhayBohra的答案,然后包括
android:paddingTop=“anydpthat you进一步想要调整”
content\u main.xml的线性布局中
信用:@Abhay Bohra和@Rahul Sharma

应用程序:布局行为
添加到您的内容\u main
线性布局中
如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
//This
app:layout_behavior="@string/appbar_scrolling_view_behavior"
android:id="@+id/textlayout"
tools:context="edu.ahduni.seas.gyapak.MainActivity">

    <GridView xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/grid"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:verticalSpacing="10dp"
        android:horizontalSpacing="10dp"
        android:gravity="center"
        android:padding="10dp" />
</LinearLayout>

尝试将appBarLayout和include语句放置在垂直方向的线性布局中

<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="edu.ahduni.seas.gyapak.MainActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <android.support.design.widget.AppBarLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            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"
                app:popupTheme="@style/AppTheme.PopupOverlay" />

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

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

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|end"
        android:layout_margin="@dimen/fab_margin"
        android:src="@android:drawable/ic_dialog_email" />

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

尝试将
工具:showIn=“@layout/activity\u main”
添加到
内容\u main.xml
线性布局中。 最终代码应如下所示:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:id="@+id/textlayout"
    tools:showIn="@layout/activity_main"
    tools:context="edu.ahduni.seas.gyapak.MainActivity">


尝试在内容的gridview中提供页边空白顶部_main@AbhayBohra它已经解决了这个问题,但我不希望它是静态的。GridView应从工具栏下方开始。尝试将paddingTop=“20dp”添加到content\u mainLinearLayout@Stallion请阅读我的上述评论。将此android:layout_marginTop=“?attr/actionBarSize”添加到您的布局中。这将解决问题。您的活动可能有theme@style/theme.AppCompat.Light.NoActionBar,这就是内容位于工具栏下的原因。@它很接近,但仍然重叠20%。不,numColumns应该自动调整可用宽度,而不应该只是垂直排列。整个UI设计应该保持不变。你说它很接近是什么意思,但仍然像20%@Mr.robott一样重叠
//这个
后面的额外一行就足够了。它会像一个符咒一样工作。这就是我在第一个答案中所建议的,它到底会做什么?PS:正如@Abhay所说的,添加包含语句也与上述答案一起工作,它应该像符咒一样工作。@Robot检查链接并跳转到布局指针。这可能有助于您理解。因此,我应该将content\u main中的线性布局更改为父协调器布局?尝试将上边距
android:Layout\u marginTop=“10dp”
添加到content\u main.xml的相同线性布局中