Android RelativeLayout高度不遵循GridView高度

我在RelativeLayout中遇到了GridView的问题,它在ScrollView中也是如此.问题是RelativeLayout的高度不遵循GridView内容的高度.当有多行时,GridView会被剪裁并出现滚动条,这是不可取的.我试图使用 Android层次结构查看器中的屏幕截图来说明我的问题.您可以看到红色RelativeLayout框如何剪切GridView的第二行.我粘贴了页面的XML布局(page.xml)和单个网格项(griditem.xml).我使用以下代码来扩展gridAdapter代码中的网格项:

/ *********** gridAdapter片段的开始********************** /

public View getView(int position,View convertView,ViewGroup parent)
{

     View v;
        if(convertView==null){
            li = LayoutInflater.from(mContext);
            v = li.inflate(R.layout.griditem,null);

            //code for fetching textView and imageUrl content   

            TextView tv = (TextView)v.findViewById(R.id.icon_text);
            tv.setText(textContent);
            ImageView iv = (ImageView)v.findViewById(R.id.icon_image);

            //code for fetching and attaching image to imageView
        }
        else
        {
            v = convertView;
        }
        return v;
}

/ *********** gridAdapter片段结尾********************** /

/ ***********启动page.xml ********************** /

<TextView android:id="@+id/title" android:layout_weight="1"

        android:layout_width="320dip"
        android:layout_height="wrap_content" 
        android:singleLine="false"
        android:textStyle="bold" 
        android:textSize="14dip" 
        />

<ImageView android:id="@+id/image"
        android:layout_below="@+id/title"
        android:adjustViewBounds="true"
        android:layout_width="128dip"
        android:layout_height="96dip" 
        android:layout_marginRight="4dip"
        />


<TextView android:id="@+id/name" 
        android:layout_weight="1"
        android:layout_below="@+id/title"
        android:layout_toRightOf="@+id/image"
        android:layout_width="192dip"
        android:layout_height="wrap_content"
        android:paddingTop="2dip"
        android:textSize="12dip" 
        android:paddingLeft="2dip"
        />

<TextView android:id="@+id/location" 
        android:layout_weight="1"
        android:layout_below="@+id/name"
        android:layout_toRightOf="@+id/image"
        android:layout_width="192dip"
        android:layout_height="wrap_content"
        android:paddingTop="2dip"
        android:textSize="12dip" 
        android:paddingLeft="2dip"
        />

<TextView android:id="@+id/date1" 
        android:layout_weight="1"
        android:layout_below="@+id/location"
        android:layout_toRightOf="@+id/image"
        android:layout_width="192dip"
        android:layout_height="wrap_content"
        android:paddingTop="2dip"
        android:textSize="10dip" 
        android:paddingLeft="2dip"
        />          

<TextView android:id="@+id/date2" 
        android:layout_weight="1"
        android:layout_below="@+id/date1"
        android:layout_toRightOf="@+id/image"
        android:layout_width="192dip"
        android:layout_height="wrap_content"
        android:paddingTop="2dip"
        android:textSize="10dip" 
        android:paddingLeft="2dip"
        />


<Button android:id="@+id/button1"
        android:text="buttonText1"
        android:layout_weight="1"
        android:layout_below="@+id/image"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:layout_marginLeft="96dip"       
        />

<Button android:id="@+id/button2"
        android:text="buttonText2"
        android:layout_weight="1"
        android:layout_below="@+id/image"
        android:layout_toRightOf="@+id/button1"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        />

<Button android:id="@+id/button3"
        android:text="Text"
        android:layout_weight="1"
        android:layout_below="@+id/image"
        android:layout_toRightOf="@+id/button2"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        />

<GridView xmlns:android="http://schemas.android.com/apk/res/android" 
        android:id="@+id/gridview"
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent"
        android:layout_below="@+id/button1"
        android:layout_weight="1"

        android:numColumns="auto_fit"
        android:verticalSpacing="10dip"
        android:horizontalSpacing="10dip"
        android:stretchMode="columnWidth"
        android:gravity="center"
        />

/ ***********结束页面文件********************** /

/ ***********启动griditem.xml ********************** /

机器人:重力= “CENTER_HORIZONTAL” >

/ ***********结束griditem.xml ********************** /

你能告诉我应该怎样做才能让RelativeLayout的高度跟随gridView的全长?

谢谢,
Kuntal![替代文字] [1]

这是截图:
http://tinypic.com/r/98rs4n/4

解决方法

到目前为止,我能够解决这个问题的唯一方法是有点软糖.

一旦知道将要加载到网格中的项目数量,就可以手动设置高度:

final int gridWidth = myGrid.getWidth();
final int cellWidthDP = 50;
final int cellHeightDP = 80;

final Resources r = getResources();
final double cellWidthPx = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,cellWidthDP,r.getDisplayMetrics());
final double cellHeightPx = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,cellHeightDP,r.getDisplayMetrics());

final int itemsPerRow = (int) Math.floor((double) gridWidth / cellWidthPx);
final int rowCount = (int) Math.ceil((double) itemCount / itemsPerRow);

final LayoutParams prm = myGrid.getLayoutParams();
prm.height = (int) Math.ceil((double) rowCount * cellHeightPx);

myGrid.setLayoutParams(prm);

相关文章

ADB Remote ATV Android TV 的遥控器,基于 ADB Shell 命令 ...
使用Flutter自带的SearchDelegate组件实现搜索界面,通过魔改...
上篇文章讲解了怎么使用Kotlin的协程配合Retrofit发起网络请...
安卓开发——WebView+Recyclerview文章详情页,解决高度...
Android 如何解决dialog弹出时无法捕捉Activity的back事件 在...