【问题标题】:GridView with square items in android with adaptive width/height具有自适应宽度/高度的android中带有方形项目的GridView
【发布时间】:2013-11-11 18:57:10
【问题描述】:

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

<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方法中手动设置项目的宽度和高度,这更好,但仍然不是我想要的。我怎样才能摆脱列之间的间距?

【问题讨论】:

  • 查看我关于如何管理 GridView stackoverflow.com/a/13835812/833622的答案
  • 您的建议基本上是手动计算项目的高度和宽度,然后将此值传递给适配器,对吗?虽然这可能会奏效,但恕我直言,这是一个尴尬的解决方案。
  • 实际上你只会在gridview被测量后才设置适配器。它与使用 GlobalLayoutListener 相同。一旦测量了gridview,您就可以准确计算所有内容。
  • 我可能应该实现我自己的自定义适配器视图,它会有我想要的属性。不幸的是,这需要相对较长的时间,因为我以前没有这样做过。

标签: android gridview


【解决方案1】:

首先,您要创建一个可以使用的自定义 View 类,而不是您正在使用的默认 LinearLayout。然后你想覆盖 View 的 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,这样您的图像就不会自行拉伸并保持其纵横比。而且,只要是方形图像,无论如何都不应裁剪。

【讨论】:

    【解决方案2】:

    所以你需要GridViewstretchMode="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       
           }
       }
    }
    

    【讨论】:

    • 不工作。在横向模式下,它占据了所有屏幕宽度,只能看到 3 行。
    • 看起来像这样。 dl.dropboxusercontent.com/u/28070056/… 并按照您的建议实施,它会在 layoutParams 上引发空指针异常。
    猜你喜欢
    • 2014-02-16
    • 2013-09-20
    • 2018-10-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多