8

我有一个带有类似项目的自定义网格视图

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="false"
android:focusable="false"
android:focusableInTouchMode="false"
android:gravity="center"
android:longClickable="false"
android:orientation="vertical" >

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:clickable="false"
    android:focusable="false"
    android:focusableInTouchMode="false"
    android:longClickable="false"
    android:text="0"
    android:textSize="60sp" />

 </LinearLayout>

我希望我的项目是正方形,我希望 gridview 拉伸宽度以填充纵向屏幕的所有宽度和横向的所有高度。它应该看起来像这样 布局

其中 A - 是正方形的边, B 是边距宽度(可能为零)。我认为我可能应该重写 onMeasure 方法,但我究竟应该怎么做?也许任何人都可以提供帮助?

编辑好的,我尝试在适配器的 getView 方法中手动设置项目的宽度和高度,这更好,但这仍然不是我想要的。我怎样才能摆脱列之间的间距?

在此处输入图像描述

4

2 回答 2

3

首先,您要创建一个可以使用的自定义 View 类,而不是您正在使用的默认 LinearLayout。然后你想覆盖视图的 onMeasure 调用,并强制它是方形的:

public class GridViewItem extends ImageView {

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

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

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

  @Override
  public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
      super.onMeasure(widthMeasureSpec, widthMeasureSpec); // This is the   key that will make the height equivalent to its width
  }
}

然后您可以将 row_grid.xml 文件更改为:

<path.to.item.GridViewItem xmlns:android="http://schemas.android.com/apk/res/android"
   android:id="@+id/item_image"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:scaleType="centerCrop"
   android:src="@drawable/ic_launcher" >
</path.to.item.GridViewItem>

请务必将“path.to.item”更改为您的 GridViewItem.java 类所在的包。

编辑:

还将 scaleType 从 fitXY 更改为 centerCrop,这样您的图像就不会自行拉伸并保持其纵横比。而且,只要它是方形图像,无论如何都不应裁剪任何内容。

于 2015-02-05T10:59:42.540 回答
-1

所以你需要GridView适合屏幕的stretchMode="columnWidth"stretchMode="rowWidth"。不幸的是,最后一个不是真正的属性,您实际上需要在运行时进行此计算,正如 Sherif elKhatib 建议的那样。

GridView 适合屏幕并自动为您拉伸列。

<GridView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:stretchMode="columnWidth" />

所以我们只需要拉伸行。如果我们知道 GridView 和 rowsCount 的高度,我们可以通过计算 rowHeight 应该是多少来做到这一点。

public class YourAdapter extends BaseAdapter {

   int rowsCount;

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

       View itemView = convertView;
       if (itemView == null) {
           itemView = layoutInflater.inflate(R.layout.item_view, parent, false);            
           int height = parent.getHeight();
           if (height > 0) {
               LayoutParams layoutParams = itemView.getLayoutParams();
               layoutParams.height = (int) (height / rowsCount);
           } // for the 1st item parent.getHeight() is not calculated yet       
       }
   }
}
于 2013-11-01T18:10:07.303 回答