具有自适应宽度/高度的Android正方形项的GridView

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>

我希望我的项目可以呈正方形,而且在纵向方向上要填满整个屏幕的宽度,在横向方向上要填满整个屏幕的高度。 效果应该如下图所示: layout 其中,A是正方形的边长,B是边距(可以为零)。 我认为我应该重写onMeasure方法,但具体应该做些什么呢? 也许有人能帮忙吗?
编辑 好的,我尝试在adapter的getView方法中手动设置项目的宽度和高度,效果不错,但仍然不是我想要的。我怎样才能消除列之间的间隔呢? 如下图所示: enter image description here

1
请查看我在如何管理GridView方面的答案:https://dev59.com/n2vXa4cB1Zd3GeqPK5oQ#13835812 - Sherif elKhatib
你的建议是手动计算一个项目的高度和宽度,然后将这些值传递给适配器,对吗? 虽然这可能会起作用,但在我看来,这是一种笨拙的解决方案。 - user1685095
实际上,只有在测量完GridView之后才能设置适配器。这与使用GlobalLayoutListener相同。一旦GridView被测量,您就可以精确地计算所有内容。 - Sherif elKhatib
我可能应该实现自己的自定义适配器视图,这样就可以拥有我想要的属性。不幸的是,这需要相对较长的时间,因为我以前没有做过这样的事情。 - user1685095
尝试使用以下链接:https://dev59.com/rmw15IYBdhLWcg3whMK_ - Deni Erdyneev
2个回答

3
首先,您需要创建一个自定义的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,以便您的图像不会被拉伸并保持其纵横比。只要是正方形图像,就不应该裁剪任何内容。


-1

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

GridView可以自动适应屏幕并拉伸列。

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

所以我们只需要拉伸行。我们可以通过计算如果我们知道GridView的高度和行数,就可以知道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行。 - user1685095
看起来像这样。https://dl.dropboxusercontent.com/u/28070056/Screenshot_2013-11-01-22-52-34.png 而且按照您的建议实现时,它会在layoutParams上抛出空指针异常。 - user1685095

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接