我有一个显示5 Activity
的GridViews
。这些GridViews
中的每一个都由一个TextView
除以背景,该背景横跨屏幕的整个宽度。 GridViews
代表一卷书,并且网格中的每个项目都是一个Button
代表一卷书。
所有GridViews
和TextViews
都位于RelativeLayout
内。有些卷有很多书,而另一些则只有几本书,因此我一直在尝试将各个网格视图放入wrap_content
,并占用显示所有书所需的所有空间,但是没有运气。 View
将空间平均分配给每个GridViews
,从而隐藏了大部分书籍。我在下面有我的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>
最佳答案
尝试通过分配GridView权重= 0.5。我几天前也面临同样的情况。 Layout的一个简单更改对我来说有用。
只需在GridView的左侧和右侧添加两个权重为1的空视图,然后为GridView分配0.5的权重即可。例如。
<LinearLayout
android:layout_width="fill_parent"
android:gravity="center"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<View
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1" />
<GridView
android:id="@+id/myGridView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:numColumns="2"
android:scrollbars="none"
android:verticalSpacing="10dp" >
</GridView>
<View
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1" />
</LinearLayout>
`