使用垂直图像调整网格大小-android

使用垂直图像调整网格大小-android,android,layout,resize,Android,Layout,Resize,我需要的图像有容器大小“GridLayout”根据大小 只是抓住他们的水平尺寸,但我也需要垂直 我错过了什么?谢谢 还有其他更简单的方法吗?我试过用新衣服,但没有成功 ------更新---- 您可以使用方形图像视图来正确调整网格项目的大小 创建一个单独的类,用于动态定义ImageView的大小,该类应继承ImageView类,如下所示: public class SquareImageView extends ImageView { public SquareImage

我需要的图像有容器大小“GridLayout”根据大小

只是抓住他们的水平尺寸,但我也需要垂直

我错过了什么?谢谢


还有其他更简单的方法吗?我试过用新衣服,但没有成功

------更新----


您可以使用方形图像视图来正确调整网格项目的大小

创建一个单独的类,用于动态定义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);

    }
}
<com.packagepath.SquareImageView
    android:id="@+id/Imageview"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" />
设置的测量尺寸(宽度、宽度);将自动将您的高度设置为宽度

在xml文件中,将此类用作视图,以代替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);

    }
}
<com.packagepath.SquareImageView
    android:id="@+id/Imageview"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" />

您可以使用方形图像视图来正确调整网格项目的大小

创建一个单独的类,用于动态定义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);

    }
}
<com.packagepath.SquareImageView
    android:id="@+id/Imageview"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" />
设置的测量尺寸(宽度、宽度);将自动将您的高度设置为宽度

在xml文件中,将此类用作视图,以代替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);

    }
}
<com.packagepath.SquareImageView
    android:id="@+id/Imageview"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" />


如果容器是较小的网格,则必须相应地确定一个比率。您给出的容器大小要求每行至少有4个,这样看起来才像样。我自己也经常遇到这种情况。我希望我已经说清楚了。现在我还有一个小问题。。。。。android layout\u columnWeight=“1”有什么替代方案?API仅适用于>21。。。。我需要>16您是否尝试删除该语句并在每个线性布局中指定高度作为包裹内容?我想这会有用的。线性布局有扭曲的内容。。。。。移除布局时,线性布局缺失:-SOh。。我对上述答案不满意。那是我说的。无论如何,我建议您创建一个自定义栅格视图,在其中可以控制宽度和高度,从而避免使用columWeight属性。下面是一个例子:-如果容器是一个较小的网格,那么您必须相应地确定一个比率。您给出的容器大小要求每行至少有4个,这样看起来才像样。我自己也经常遇到这种情况。我希望我已经说清楚了。现在我还有一个小问题。。。。。android layout\u columnWeight=“1”有什么替代方案?API仅适用于>21。。。。我需要>16您是否尝试删除该语句并在每个线性布局中指定高度作为包裹内容?我想这会有用的。线性布局有扭曲的内容。。。。。移除布局时,线性布局缺失:-SOh。。我对上述答案不满意。那是我说的。无论如何,我建议您创建一个自定义栅格视图,在其中可以控制宽度和高度,从而避免使用columWeight属性。以下是一个例子:-