带有图像的GridLayout屏幕

时间:2017-05-23 17:50:50

标签: android android-layout android-gridlayout

我试图用图片创建GridLayout。网格有3列。

我有一些问题:

  1. ImageView s离开屏幕,如下图所示。 GridLayout并未正确设置ImageView的宽度以适应。
  2. enter image description here

    1. ImageView之间没有空格/边距,即使我为horizontalSpacing设置了verticalSpacingGridLayout属性。
    2. 这是我目前的代码。我应该改变什么来解决我的两个问题?

      <GridLayout
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:columnCount="3"
          android:horizontalSpacing="10dp"
          android:verticalSpacing="10dp"
          android:stretchMode="columnWidth">
      
          <ImageView
              android:layout_width="130dp"
              android:layout_height="130dp"
              android:background="@color/Color_DarkRed" />
      
          <ImageView
              android:layout_width="130dp"
              android:layout_height="130dp"
              android:background="@color/Color_DarkRed" />
      
          <ImageView
              android:layout_width="130dp"
              android:layout_height="130dp"
              android:background="@color/Color_DarkRed" />
      
          <ImageView
              android:layout_width="130dp"
              android:layout_height="130dp"
              android:background="@color/Color_DarkRed" />
      
          <ImageView
              android:layout_width="130dp"
              android:layout_height="130dp"
              android:background="@color/Color_DarkRed" />
      
          <ImageView
              android:layout_width="130dp"
              android:layout_height="130dp"
              android:background="@color/Color_DarkRed" />
      
      </GridLayout>
      

      更新

      关注this answer后,我尝试使用以下布局:

      <GridLayout
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:columnCount="3">
      
          <com.my.package.view.SquareImageView
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:background="@color/Color_DarkRed" />
      
          <com.my.package.view.SquareImageView
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:background="@color/Color_DarkRed" />
      
          <com.my.package.view.SquareImageView
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:background="@color/Color_DarkRed" />
      
      </GridLayout>
      

      但它看起来像下图。每个ImageView占据屏幕的整个宽度:

      http://i.imgur.com/p8AXcKN.png

2 个答案:

答案 0 :(得分:0)

你应该减少layout_width =“130dp”,因为你的屏幕不能保证是390dp

答案 1 :(得分:0)

更改图像,或者您也可以使用方形图像视图来正确调整网格项的大小。

创建一个单独的类,用于为ImageView动态定义应继承ImageView类的大小,如下所示:

    public class SquareImageView extends ImageView {

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

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

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

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, widthMeasureSpec);

    int width = getMeasuredWidth();
        setMeasuredDimension(width, width);

    }
}

setMeasuredDimension(宽度,宽度);将自动将您的身高设置为宽度。

在xml文件中,使用此类作为视图来代替ImageView,如下所示:

<com.packagepath.SquareImageView
    android:id="@+id/Imageview"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" />
相关问题