我想在GridView的顶部和底部添加空白区域,类似于头部/尾部,但是我只想支持空白,不想支持其他元素。
目前我正在使用顶部/底部填充,但当滚动内容时,内容将在填充部分被裁剪。
有什么最简单的解决方案吗?谢谢!
如果我理解你的意思正确,它应该是这样的:
Maciej在这里提到了一个简单的解决方案:如何在GridView底部添加额外的空间
您只需要在GridView布局中添加以下内容:
<GridView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingTop="50dp"
android:paddingBottom="50dp"
android:clipToPadding="false"/>
最重要的部分是 clipToPadding 属性,必须设置为 false。
您还可以查看 Google 的相关博客文章,在其中提到了此解决方案:https://plus.google.com/+AndroidDevelopers/posts/LpAA7q4jw9M
不要使用 内边距(在视图内添加空间),而是使用 外边距(在视图外添加空间):
添加
android:layout_marginTop="40dp"
android:layout_marginBottom="40dp"
将内容添加到您的GridView(或其他值)
[编辑]
根据OP的澄清:必须固定“空格”,使所有GridView可滚动。
因此,我会这样设计它(通过设置一对锚定的TextView作为固定的页眉和页脚):
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/black"
android:padding="8dp"
>
<!-- Header -->
<TextView
android:id="@+id/txtHeader"
android:layout_width="match_parent"
android:layout_height="48dp"
android:layout_alignParentTop="true"
android:background="#ff00"
android:gravity="center"
android:text="Header"
android:textColor="@android:color/white"
android:textSize="24sp"
android:textStyle="bold"
/>
<!-- Footer -->
<TextView
android:id="@+id/txtFooter"
android:layout_width="match_parent"
android:layout_height="48dp"
android:layout_alignParentBottom="true"
android:background="#ff00"
android:gravity="center"
android:text="Footer"
android:textColor="@android:color/white"
android:textSize="24sp"
android:textStyle="bold"
/>
<!-- The Grid -->
<!-- <GridView -->
<!-- android:id="@+id/grdIcons" -->
<!-- android:layout_width="match_parent" -->
<!-- android:layout_height="match_parent" -->
<!-- android:layout_above="@id/txtFooter" -->
<!-- android:layout_below="@id/txtHeader" -->
<!-- android:background="#f0f0" -->
<!-- android:textColor="@android:color/white" -->
<!-- android:textSize="24sp" -->
<!-- android:textStyle="bold" -->
<!-- /> -->
<GridView
android:id="@+id/grdIcons"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@id/txtFooter"
android:layout_below="@id/txtHeader"
android:background="#f00f"
android:textColor="@android:color/white"
android:textSize="24sp"
android:textStyle="bold"
android:columnWidth="64dp"
android:numColumns="auto_fit"
android:verticalSpacing="8dp"
android:horizontalSpacing="8dp"
android:stretchMode="none"
android:gravity="center"
/>
</RelativeLayout>
gridview
放入一个与父布局相匹配宽度和高度的Relative layout
中,然后给出top
和bottom margin
。 - Bills