当前位置: 首页 > 面试题库 >

Gridview的高度被削减

秦毅
2023-03-14
问题内容

我正在尝试在gridview中显示8个项目。可悲的是,gridview的高度总是太小,因此它仅显示第一行,而第二行的一小部分。

设置android:layout_height="300dp"使它起作用。wrap_ content,fill_parent显然不是。

我的网格视图:

<GridView
    android:id="@+id/myId"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:horizontalSpacing="2dp"
    android:isScrollContainer="false"
    android:numColumns="4"
    android:stretchMode="columnWidth"
    android:verticalSpacing="20dp" />

我的物品资源:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:minHeight="?android:attr/listPreferredItemHeight" >

    <ImageView
        android:id="@+id/appItemIcon"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:src="@android:drawable/ic_dialog_info"
        android:scaleType="center" />      

    <TextView
        android:id="@+id/appItemText"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="My long application name"
        android:gravity="center_horizontal"
        android:textAppearance="?android:attr/textAppearanceSmall" />

</LinearLayout>

该问题似乎与缺乏垂直空间无关。

我能做什么 ?


问题答案:

经过(太多)研究,我偶然发现了尼尔·特拉夫特(Neil Traft)的出色回答。

适应他的工作GridView已经很容易了。

ExpandableHeightGridView.java:

package com.example;
public class ExpandableHeightGridView extends GridView
{

    boolean expanded = false;

    public ExpandableHeightGridView(Context context)
    {
        super(context);
    }

    public ExpandableHeightGridView(Context context, AttributeSet attrs)
    {
        super(context, attrs);
    }

    public ExpandableHeightGridView(Context context, AttributeSet attrs,
            int defStyle)
    {
        super(context, attrs, defStyle);
    }

    public boolean isExpanded()
    {
        return expanded;
    }

    @Override
    public void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
    {
        // HACK! TAKE THAT ANDROID!
        if (isExpanded())
        {
            // Calculate entire height by providing a very large height hint.
            // View.MEASURED_SIZE_MASK represents the largest height possible.
            int expandSpec = MeasureSpec.makeMeasureSpec(MEASURED_SIZE_MASK,
                    MeasureSpec.AT_MOST);
            super.onMeasure(widthMeasureSpec, expandSpec);

            ViewGroup.LayoutParams params = getLayoutParams();
            params.height = getMeasuredHeight();
        }
        else
        {
            super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        }
    }

    public void setExpanded(boolean expanded)
    {
        this.expanded = expanded;
    }
}

将其包含在您的布局中,如下所示:

<com.example.ExpandableHeightGridView
    android:id="@+id/myId"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:horizontalSpacing="2dp"
    android:isScrollContainer="false"
    android:numColumns="4"
    android:stretchMode="columnWidth"
    android:verticalSpacing="20dp" />

最后,您只需要让它扩展即可:

mAppsGrid = (ExpandableHeightGridView) findViewById(R.id.myId);
mAppsGrid.setExpanded(true);


 类似资料:
  • 我按照以下指南使用网格视图在图库中插入图像:http://developer . Android . com/guide/topics/ui/layout/GridView . html 我编辑了imageadapter代码,如下所示: } 这是img_gallery个 xml 这是主要布局: 问题是,当我调整应用程序时,imageview有很多顶部和底部填充。我也尝试在xml中将其设置为0,但它

  • 让我先给你介绍一下我的项目。我有一个pdf,我需要使用PDFBox API将其转换为图像(一张图像对应一页),并使用PDFBox API将所有这些图像写入一个新的pdf。基本上,将pdf转换为pdf,我们称之为pdf转码。 现在,我有一些情况下,每件事都进展顺利,也就是说,转码pdf与原始pdf内容完全匹配,但只有1/10的情况是这样的,转码pdf的尺寸以某种方式被交换。例如,原始pdf-8.2x

  • 我正在尝试编码一个。mp4视频来自使用FFMPEG的一组帧,使用libx264编解码器。 这是我正在运行的命令: 我有时会遇到以下错误: 在搜索了一段时间后,这个问题似乎与缩放算法有关,可以通过添加-vf参数来解决。 然而,在我的情况下,我不想做任何缩放。理想情况下,我希望保持尺寸与框架完全相同。有什么建议吗?是否存在h264强制执行的某种纵横比?

  • 问题内容: 我使用此 CSS将最大高度设置为 谁能给我一个普遍的答案, 和 之间有什么区别? 问题答案: 这是W3Clink的解释: 以下算法描述了两个属性[min-height和max-height]如何影响’height’属性的使用值: 临时使用的高度是根据上面“计算高度和边距”下的规则计算的(不包括“最小高度”和“最大高度”)。 如果此暂定高度大于“最大高度”,则会再次应用上述规则,但是这次

  • 组件的高度和宽度决定了其在屏幕上显示的尺寸。 指定宽高 最简单的给组件设定尺寸的方式就是在样式中指定固定的 width 和 height 。React Native 中的尺寸都是无单位的,表示的是与设备像素密度无关的逻辑像素点。 export default class HelloWorld extends Component { render() { return ( <

  • 问题内容: 即使在指定Container GridView的高度之后,我的代码仍在生成方形小部件。 上面代码的输出如左图所示。如何获得带有自定义高度小部件的GridView ,如右图所示? 问题答案: 关键是。这个值是用来确定的布局在。为了获得所需的外观,您必须将其设置为 ()。解决方案是这样的: