多个android GridViews不会wrap_content

发布于 2024-07-24 08:57:41 字数 6816 浏览 6 评论 0原文

我有一个显示 5 个 GridView 的 Activity。 每个 GridViews 都被一个背景横跨屏幕宽度的 TextView 划分。 GridViews 代表一卷书,网格中的每个项目都是一个 Button 代表卷中的一本书。

所有 GridViewsTextViews 都位于 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

七秒鱼° 2024-07-31 08:57:42

设置这两个标签。

android:layout_width="wrap_content"
android:layout_height="wrap_content"

在所有 Gridview 中

set both

android:layout_width="wrap_content"
android:layout_height="wrap_content"

tag in All Gridview.

花开柳相依 2024-07-31 08:57:41

尝试分配 GridView 权重 = 0.5。 几天前我也面临同样的情况。 布局的简单更改对我有用。
只需在 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>

`

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.

<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>

`

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文