网格视图在工具栏前显示不低于

问题描述:

我只是用工具栏和网格视图创建一个简单的xml文件,问题在于工具栏的网格视图显示信息不在工具栏下方,像这样 Grid View网格视图在工具栏前显示不低于

,这是我的xml文件

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    tools:context="abtech.waiteriano.com.waitrer.MenuActivity"> 

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

    <GridView 
     android:id="@+id/menuGridView" 
     android:layout_width="match_parent" 
     android:background="#d2d2d2" 
     android:layout_height="fill_parent" 
     android:layout_margin="4dp" 
     android:columnWidth="80dp" 
     android:gravity="center" 
     android:horizontalSpacing="5dp" 
     android:numColumns="3" 
     android:stretchMode="columnWidth" 
     android:layout_below="@+id/toolbar" /> 
</FrameLayout> 

你有diferent选项

  • 更改您的FrameLayout为的LinearLayout(方向垂直)

  • 更改您的FrameLayout为RelativeLayout的,并使用 'layout_below'

我建议你使用的LinearLayout

示例:

的LinearLayout

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:orientation="vertical" 
    tools:context="abtech.waiteriano.com.waitrer.MenuActivity"> 

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

    <GridView 
     android:id="@+id/menuGridView" 
     android:layout_width="match_parent" 
     android:background="#d2d2d2" 
     android:layout_height="fill_parent" 
     android:layout_margin="4dp" 
     android:columnWidth="80dp" 
     android:gravity="center" 
     android:horizontalSpacing="5dp" 
     android:numColumns="3" 
     android:stretchMode="columnWidth" /> 
</LinearLayout> 

的RelativeLayout

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    tools:context="abtech.waiteriano.com.waitrer.MenuActivity"> 

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

    <GridView 
     android:id="@+id/menuGridView" 
     android:layout_width="match_parent" 
     android:background="#d2d2d2" 
     android:layout_height="fill_parent" 
     android:layout_margin="4dp" 
     android:columnWidth="80dp" 
     android:gravity="center" 
     android:horizontalSpacing="5dp" 
     android:numColumns="3" 
     android:stretchMode="columnWidth" 
     android:layout_below="@id/toolbar" /> 
</RelativeLayout> 

变化FrameLayoutLinearLayout并设置方向垂直:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:orientation="vertical" 
    tools:context="abtech.waiteriano.com.waitrer.MenuActivity"> 

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

    <GridView 
     android:id="@+id/menuGridView" 
     android:layout_width="match_parent" 
     android:background="#d2d2d2" 
     android:layout_height="fill_parent" 
     android:layout_margin="4dp" 
     android:columnWidth="80dp" 
     android:gravity="center" 
     android:horizontalSpacing="5dp" 
     android:numColumns="3" 
     android:stretchMode="columnWidth" 
     android:layout_below="@+id/toolbar" /> 
</LinearLayout > 
+1

OMG由于它是非常简单的错误非常感谢@Nika Kurdadze –

的FrameLayout设计为显示单个项目。

通常应该使用FrameLayout来保存单个子视图。

但是,您可以将多个子项添加到FrameLayout并控制其在FrameLayout中的位置。

所以,如果你想继续使用FrameLayout,那么你可以在FrameLayout中添加LinearLayout或者RelativeLayout,如下所示。

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

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="wrap_content" 
android:orientation="vertical"> 

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

<GridView 
    android:id="@+id/menuGridView" 
    android:layout_width="match_parent" 
    android:background="#d2d2d2" 
    android:layout_height="fill_parent" 
    android:layout_margin="4dp" 
    android:columnWidth="80dp" 
    android:gravity="center" 
    android:horizontalSpacing="5dp" 
    android:numColumns="3" 
    android:stretchMode="columnWidth" 
    android:layout_below="@+id/toolbar" />