多个android GridViews不会wrap_content
我有一个显示 5 个 GridView 的 Activity
。 每个 GridViews 都被一个背景横跨屏幕宽度的 TextView 划分。 GridViews
代表一卷书,网格中的每个项目都是一个 Button
代表卷中的一本书。
所有 GridViews
和 TextViews
都位于 RelativeLayout
内。 有些卷有很多书,而另一些卷只有几本书,所以我一直在尝试将各个网格视图放入 wrap_content
并占用他们显示所有书籍所需的所有空间,但没有运气。 View
将空间平均分配给每个 GridView
,隐藏了大部分书籍。 我的 xml 布局如下。 谁能告诉我如何完成这件事?
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:as="http://schemas.android.com/apk/res/edu.byu.scriptures"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<RelativeLayout android:id="@+id/gridNavBar"
android:layout_height="44px"
android:layout_width="fill_parent"
android:background="@drawable/bgnd_1_44"
android:paddingBottom="0px">
<Button
android:id="@+id/homeButton"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_marginLeft="5px"
android:text="Home"
android:textSize="14sp"
android:textColor="#fff"
android:shadowColor="#444"
android:shadowRadius="1"
android:shadowDx="1"
android:shadowDy="-1"
android:background="@drawable/generic_button_xml"/>
<TextView android:id="@+id/bookRef"
android:text="Books"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:textColor="#fff"
android:textSize="18sp"
android:textStyle="bold"
android:shadowColor="#444"
android:shadowRadius="1"
android:shadowDx="1"
android:shadowDy="-1"
/>
</RelativeLayout>
<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_below="@id/gridNavBar">
<RelativeLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView
android:gravity="top"
android:id="@+id/vol1DividerBar"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Volume 1"
android:textColor="#000"
android:background="@drawable/volume_divider"
android:layout_centerHorizontal="true"
android:layout_alignParentTop="true"/>
<GridView
android:layout_weight="1"
android:id="@+id/vol1Grid"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:numColumns="auto_fit"
android:verticalSpacing="0dp"
android:horizontalSpacing="0dp"
android:layout_below="@id/vol1DividerBar"/>
<TextView
android:layout_weight="1"
android:id="@+id/vol2DividerBar"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Volume 2"
android:textColor="#000"
android:background="@drawable/volume_divider"
android:layout_below="@id/vol1Grid"/>
<GridView
android:layout_weight="1"
android:id="@+id/vol2Grid"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:numColumns="auto_fit"
android:verticalSpacing="0dp"
android:horizontalSpacing="0dp"
android:layout_below="@id/vol2DividerBar"/>
<TextView
android:layout_weight="1"
android:id="@+id/vol3DividerBar"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Volume 3"
android:textColor="#000"
android:background="@drawable/volume_divider"
android:layout_below="@id/vol2Grid"/>
<GridView
android:layout_weight="1"
android:id="@+id/vol3Grid"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:numColumns="auto_fit"
android:verticalSpacing="0dp"
android:horizontalSpacing="0dp"
android:layout_below="@id/vol3DividerBar"/>
<TextView
android:layout_weight="1"
android:id="@+id/vol4DividerBar"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="The Doctrine and Covenants"
android:textColor="#000"
android:background="@drawable/volume_divider"
android:layout_below="@id/vol3Grid"/>
<GridView
android:layout_weight="1"
android:id="@+id/vol4Grid"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:numColumns="auto_fit"
android:verticalSpacing="0dp"
android:horizontalSpacing="0dp"
android:layout_below="@id/vol4DividerBar"/>
<TextView
android:layout_weight="1"
android:id="@+id/vol5DividerBar"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="The Pearl of Great Price"
android:textColor="#000"
android:background="@drawable/volume_divider"
android:layout_below="@id/vol4Grid"/>
<GridView
android:layout_weight="1"
android:id="@+id/vol5Grid"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:numColumns="auto_fit"
android:verticalSpacing="0dp"
android:horizontalSpacing="0dp"
android:layout_below="@id/vol5DividerBar"/>
</RelativeLayout>
</ScrollView>
</RelativeLayout>
I've got an Activity
that displays 5GridViews
. Each of these GridViews
is divided by a TextView
with a background that spans the width of the screen. The GridViews
represent a volume of books, and each item in the grid is a Button
representing a book in a volume.
All the GridViews
and TextViews
sit inside a RelativeLayout
. Some Volumes have many books and others just a few, so I've been trying to get the individual grid views to wrap_content
and take up all the space they need to show all of their books, but no luck. The View
equally divides space to each of the GridViews
, hiding most of the books. I have my xml layout below. Can anyone tell me how I can get this done?
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:as="http://schemas.android.com/apk/res/edu.byu.scriptures"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<RelativeLayout android:id="@+id/gridNavBar"
android:layout_height="44px"
android:layout_width="fill_parent"
android:background="@drawable/bgnd_1_44"
android:paddingBottom="0px">
<Button
android:id="@+id/homeButton"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_marginLeft="5px"
android:text="Home"
android:textSize="14sp"
android:textColor="#fff"
android:shadowColor="#444"
android:shadowRadius="1"
android:shadowDx="1"
android:shadowDy="-1"
android:background="@drawable/generic_button_xml"/>
<TextView android:id="@+id/bookRef"
android:text="Books"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:textColor="#fff"
android:textSize="18sp"
android:textStyle="bold"
android:shadowColor="#444"
android:shadowRadius="1"
android:shadowDx="1"
android:shadowDy="-1"
/>
</RelativeLayout>
<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_below="@id/gridNavBar">
<RelativeLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView
android:gravity="top"
android:id="@+id/vol1DividerBar"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Volume 1"
android:textColor="#000"
android:background="@drawable/volume_divider"
android:layout_centerHorizontal="true"
android:layout_alignParentTop="true"/>
<GridView
android:layout_weight="1"
android:id="@+id/vol1Grid"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:numColumns="auto_fit"
android:verticalSpacing="0dp"
android:horizontalSpacing="0dp"
android:layout_below="@id/vol1DividerBar"/>
<TextView
android:layout_weight="1"
android:id="@+id/vol2DividerBar"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Volume 2"
android:textColor="#000"
android:background="@drawable/volume_divider"
android:layout_below="@id/vol1Grid"/>
<GridView
android:layout_weight="1"
android:id="@+id/vol2Grid"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:numColumns="auto_fit"
android:verticalSpacing="0dp"
android:horizontalSpacing="0dp"
android:layout_below="@id/vol2DividerBar"/>
<TextView
android:layout_weight="1"
android:id="@+id/vol3DividerBar"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Volume 3"
android:textColor="#000"
android:background="@drawable/volume_divider"
android:layout_below="@id/vol2Grid"/>
<GridView
android:layout_weight="1"
android:id="@+id/vol3Grid"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:numColumns="auto_fit"
android:verticalSpacing="0dp"
android:horizontalSpacing="0dp"
android:layout_below="@id/vol3DividerBar"/>
<TextView
android:layout_weight="1"
android:id="@+id/vol4DividerBar"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="The Doctrine and Covenants"
android:textColor="#000"
android:background="@drawable/volume_divider"
android:layout_below="@id/vol3Grid"/>
<GridView
android:layout_weight="1"
android:id="@+id/vol4Grid"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:numColumns="auto_fit"
android:verticalSpacing="0dp"
android:horizontalSpacing="0dp"
android:layout_below="@id/vol4DividerBar"/>
<TextView
android:layout_weight="1"
android:id="@+id/vol5DividerBar"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="The Pearl of Great Price"
android:textColor="#000"
android:background="@drawable/volume_divider"
android:layout_below="@id/vol4Grid"/>
<GridView
android:layout_weight="1"
android:id="@+id/vol5Grid"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:numColumns="auto_fit"
android:verticalSpacing="0dp"
android:horizontalSpacing="0dp"
android:layout_below="@id/vol5DividerBar"/>
</RelativeLayout>
</ScrollView>
</RelativeLayout>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
设置这两个标签。
在所有 Gridview 中
set both
tag in All Gridview.
尝试分配 GridView 权重 = 0.5。 几天前我也面临同样的情况。 布局的简单更改对我有用。
只需在 GridView 的左侧和右侧添加两个空视图,权重 = 1,并为 GridView 分配 0.5 权重。 例如。
`
Try by assigning GridView Weight = 0.5. I face same situation a few days ago. A simple change in Layout work for me.
Simply add two empty views at left and right of my GridView with weight = 1 and assign 0.5 weight to GridView. Eg.
`