【问题标题】:Multiple android GridViews won't wrap_content多个 android GridViews 不会 wrap_content
【发布时间】:2010-11-03 18:26:33
【问题描述】:

我有一个显示 5GridViewsActivity。这些GridViews 中的每一个都被TextView 分割,其背景跨越屏幕的宽度。 GridViews 代表一卷书,网格中的每一项都是一个Button,代表一卷中的一本书。

所有GridViewsTextViews 都位于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>

【问题讨论】:

    标签: android android-layout android-gridview


    【解决方案1】:

    尝试分配 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>
    

    `

    【讨论】:

      【解决方案2】:

      同时设置

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

      所有 Gridview 中的标记。

      【讨论】:

        猜你喜欢
        • 2015-04-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-11-17
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多