Android 在GridLayout中自动调整图像大小?

Android 在GridLayout中自动调整图像大小?,android,android-layout,android-imageview,android-gridlayout,Android,Android Layout,Android Imageview,Android Gridlayout,我有12个图像,我希望以4行3列的网格显示。 为此,我使用了GridLayout。 这些图像大小不同,我希望它们可以缩放,这样每个图像都可以填充一个单元格,就像线性布局中的权重一样。 我已经使用了ImageViews scaleType和adjustViewBounds属性,但仅通过手动设置layout_width和layout_height成功。 这是可能的还是我应该提前调整尺寸?有没有更好的方法通过不同的布局来实现我的目标 我正在使用Android Studio 1.1.0,API 21 谢

我有12个图像,我希望以4行3列的网格显示。 为此,我使用了GridLayout。 这些图像大小不同,我希望它们可以缩放,这样每个图像都可以填充一个单元格,就像线性布局中的权重一样。 我已经使用了ImageViews scaleType和adjustViewBounds属性,但仅通过手动设置layout_width和layout_height成功。 这是可能的还是我应该提前调整尺寸?有没有更好的方法通过不同的布局来实现我的目标

我正在使用Android Studio 1.1.0,API 21

谢谢大家!

activity_set_background.xml


}

根据我的想法,您可以使用相对和线性布局来显示Imageview,并从“值”文件夹中为受支持的设备大小设置不同样式的mergin或填充,设置在Imageview代码下面

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<LinearLayout
    android:id="@+id/first"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:weightSum="3" >

    <ImageView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"

        android:adjustViewBounds="true"
        android:background="@drawable/ic_launcher"
        android:scaleType="centerInside"
        android:text="1" />
    <ImageView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:adjustViewBounds="true"
        android:background="@drawable/ic_launcher"
        android:scaleType="centerInside"
        android:text="1" />

    <ImageView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:adjustViewBounds="true"
        android:background="@drawable/ic_launcher"
        android:scaleType="centerInside"
        android:text="1" />
</LinearLayout>
<LinearLayout
    android:id="@+id/second"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_below="@id/first"
    android:weightSum="3" >

    <ImageView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:adjustViewBounds="true"
        android:background="@drawable/ic_launcher"
        android:scaleType="centerInside"
        android:text="1" />
    <ImageView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:adjustViewBounds="true"
        android:background="@drawable/ic_launcher"
        android:scaleType="centerInside"
        android:text="1" />
    <ImageView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:adjustViewBounds="true"
        android:background="@drawable/ic_launcher"
        android:scaleType="centerInside"
        android:text="1" />
</LinearLayout>
<LinearLayout
    android:id="@+id/third"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_below="@id/second"
    android:weightSum="3" >

    <ImageView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:adjustViewBounds="true"
        android:background="@drawable/ic_launcher"
        android:scaleType="centerInside"
        android:text="1" />
    <ImageView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:adjustViewBounds="true"
        android:background="@drawable/ic_launcher"
        android:scaleType="centerInside"
        android:text="1" />

    <ImageView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:adjustViewBounds="true"
        android:background="@drawable/ic_launcher"
        android:scaleType="centerInside"
        android:text="1" />
</LinearLayout>
<LinearLayout
    android:id="@+id/fourth"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_below="@id/third"
    android:weightSum="3" >

    <ImageView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:adjustViewBounds="true"
        android:background="@drawable/ic_launcher"
        android:scaleType="centerInside"
        android:text="1" />

    <ImageView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:adjustViewBounds="true"
        android:background="@drawable/ic_launcher"
        android:scaleType="centerInside"
        android:text="1" />

    <ImageView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:adjustViewBounds="true"
        android:background="@drawable/ic_launcher"
        android:scaleType="centerInside"
        android:text="1" />
</LinearLayout>

您看过本文档了吗?它向您展示了如何通过实现ImageAdapter来设置宽度和高度
public class SquareImageView extends ImageView
{ 

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

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

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


@Override 
protected void onMeasure(final int widthMeasureSpec, final int heightMeasureSpec)
{ 
    final int width = getDefaultSize(getSuggestedMinimumWidth(),widthMeasureSpec);
    setMeasuredDimension(width, width);
} 

@Override 
protected void onSizeChanged(final int w, final int h, final int oldw, final int oldh)
{ 
    super.onSizeChanged(w, w, oldw, oldh);
} 
 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<LinearLayout
    android:id="@+id/first"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:weightSum="3" >

    <ImageView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"

        android:adjustViewBounds="true"
        android:background="@drawable/ic_launcher"
        android:scaleType="centerInside"
        android:text="1" />
    <ImageView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:adjustViewBounds="true"
        android:background="@drawable/ic_launcher"
        android:scaleType="centerInside"
        android:text="1" />

    <ImageView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:adjustViewBounds="true"
        android:background="@drawable/ic_launcher"
        android:scaleType="centerInside"
        android:text="1" />
</LinearLayout>
<LinearLayout
    android:id="@+id/second"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_below="@id/first"
    android:weightSum="3" >

    <ImageView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:adjustViewBounds="true"
        android:background="@drawable/ic_launcher"
        android:scaleType="centerInside"
        android:text="1" />
    <ImageView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:adjustViewBounds="true"
        android:background="@drawable/ic_launcher"
        android:scaleType="centerInside"
        android:text="1" />
    <ImageView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:adjustViewBounds="true"
        android:background="@drawable/ic_launcher"
        android:scaleType="centerInside"
        android:text="1" />
</LinearLayout>
<LinearLayout
    android:id="@+id/third"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_below="@id/second"
    android:weightSum="3" >

    <ImageView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:adjustViewBounds="true"
        android:background="@drawable/ic_launcher"
        android:scaleType="centerInside"
        android:text="1" />
    <ImageView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:adjustViewBounds="true"
        android:background="@drawable/ic_launcher"
        android:scaleType="centerInside"
        android:text="1" />

    <ImageView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:adjustViewBounds="true"
        android:background="@drawable/ic_launcher"
        android:scaleType="centerInside"
        android:text="1" />
</LinearLayout>
<LinearLayout
    android:id="@+id/fourth"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_below="@id/third"
    android:weightSum="3" >

    <ImageView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:adjustViewBounds="true"
        android:background="@drawable/ic_launcher"
        android:scaleType="centerInside"
        android:text="1" />

    <ImageView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:adjustViewBounds="true"
        android:background="@drawable/ic_launcher"
        android:scaleType="centerInside"
        android:text="1" />

    <ImageView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:adjustViewBounds="true"
        android:background="@drawable/ic_launcher"
        android:scaleType="centerInside"
        android:text="1" />
</LinearLayout>