在GridLayout中强制使用固定的组件大小

时间:2017-02-19 09:37:18

标签: android imageview grid-layout

我有一个4 * 4网格的ImageViews排列在GridLayout中。 Design View

当我更新ImageViews时,网格中所有项目的比例与所提供的图像成比例变化,此处显示为Android图标作为基本ImageViews,以及2个棕色更新的ImageViews。

Scaling Relative To Updates

当我尝试更新大量的ImageViews时,这种情况发生了极端情况,并且一切都在屏幕外移动

Shift offscreen

每个ImageView都以相同的方式初始化(​​行列位置除外)

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:srcCompat="@mipmap/ic_launcher"
        android:id="@+id/tileImg00"
        android:layout_row="0"
        android:layout_column="0"
        android:layout_columnWeight="1"
        android:layout_rowWeight="1"
        android:contentDescription="@string/img_descriptor" />

如何修复ImageView的大小?我想设置图像,缩放它以使其适合设计蓝图中显示的ImageView的边界,并确保图像不允许ImageView打破网格的原始方向。

完整XML:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.mrybak834.a2048.MainActivity">

    <GridLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentBottom="true"
        android:layout_alignParentStart="true"
        android:layout_marginBottom="195dp">

        <ImageView
            android:id="@+id/tileImg00"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_column="0"
            android:layout_columnWeight="1"
            android:layout_row="0"
            android:layout_rowWeight="1"
            android:contentDescription="@string/img_descriptor"
            app:srcCompat="@mipmap/ic_launcher" />

        <ImageView
            android:id="@+id/tileImg01"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_column="1"
            android:layout_columnWeight="1"
            android:layout_row="0"
            android:layout_rowWeight="1"
            android:contentDescription="@string/img_descriptor"
            app:srcCompat="@mipmap/ic_launcher" />

        <ImageView
            android:id="@+id/tileImg02"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_column="2"
            android:layout_columnWeight="1"
            android:layout_row="0"
            android:layout_rowWeight="1"
            android:adjustViewBounds="false"
            android:contentDescription="@string/img_descriptor"
            app:srcCompat="@mipmap/ic_launcher" />

        <ImageView
            android:id="@+id/tileImg03"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_column="3"
            android:layout_columnWeight="1"
            android:layout_row="0"
            android:layout_rowWeight="1"
            android:adjustViewBounds="false"
            android:contentDescription="@string/img_descriptor"
            app:srcCompat="@mipmap/ic_launcher" />

        <ImageView
            android:id="@+id/tileImg10"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_column="0"
            android:layout_columnWeight="1"
            android:layout_row="1"
            android:layout_rowWeight="1"
            android:adjustViewBounds="false"
            android:contentDescription="@string/img_descriptor"
            app:srcCompat="@mipmap/ic_launcher" />

        <ImageView
            android:id="@+id/tileImg11"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_column="1"
            android:layout_columnWeight="1"
            android:layout_row="1"
            android:layout_rowWeight="1"
            android:adjustViewBounds="false"
            android:contentDescription="@string/img_descriptor"
            app:srcCompat="@mipmap/ic_launcher" />

        <ImageView
            android:id="@+id/tileImg12"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_column="2"
            android:layout_columnWeight="1"
            android:layout_row="1"
            android:layout_rowWeight="1"
            android:adjustViewBounds="false"
            android:contentDescription="@string/img_descriptor"
            app:srcCompat="@mipmap/ic_launcher" />

        <ImageView
            android:id="@+id/tileImg13"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_column="3"
            android:layout_columnWeight="1"
            android:layout_row="1"
            android:layout_rowWeight="1"
            android:adjustViewBounds="false"
            android:contentDescription="@string/img_descriptor"
            app:srcCompat="@mipmap/ic_launcher" />

        <ImageView
            android:id="@+id/tileImg20"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_column="0"
            android:layout_columnWeight="1"
            android:layout_row="2"
            android:layout_rowWeight="1"
            android:adjustViewBounds="false"
            android:contentDescription="@string/img_descriptor"
            app:srcCompat="@mipmap/ic_launcher" />

        <ImageView
            android:id="@+id/tileImg21"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_column="1"
            android:layout_columnWeight="1"
            android:layout_row="2"
            android:layout_rowWeight="1"
            android:adjustViewBounds="false"
            android:contentDescription="@string/img_descriptor"
            app:srcCompat="@mipmap/ic_launcher" />

        <ImageView
            android:id="@+id/tileImg22"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_column="2"
            android:layout_columnWeight="1"
            android:layout_row="2"
            android:layout_rowWeight="1"
            android:adjustViewBounds="false"
            android:contentDescription="@string/img_descriptor"
            app:srcCompat="@mipmap/ic_launcher" />

        <ImageView
            android:id="@+id/tileImg23"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_column="3"
            android:layout_columnWeight="1"
            android:layout_row="2"
            android:layout_rowWeight="1"
            android:adjustViewBounds="false"
            android:contentDescription="@string/img_descriptor"
            app:srcCompat="@mipmap/ic_launcher" />

        <ImageView
            android:id="@+id/tileImg30"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_column="0"
            android:layout_columnWeight="1"
            android:layout_row="3"
            android:layout_rowWeight="1"
            android:adjustViewBounds="false"
            android:contentDescription="@string/img_descriptor"
            app:srcCompat="@mipmap/ic_launcher" />

        <ImageView
            android:id="@+id/tileImg31"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_column="1"
            android:layout_columnWeight="1"
            android:layout_row="3"
            android:layout_rowWeight="1"
            android:adjustViewBounds="false"
            android:contentDescription="@string/img_descriptor"
            app:srcCompat="@mipmap/ic_launcher" />

        <ImageView
            android:id="@+id/tileImg32"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_column="2"
            android:layout_columnWeight="1"
            android:layout_row="3"
            android:layout_rowWeight="1"
            android:adjustViewBounds="false"
            android:contentDescription="@string/img_descriptor"
            app:srcCompat="@mipmap/ic_launcher" />

        <ImageView
            android:id="@+id/tileImg33"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_column="3"
            android:layout_columnWeight="1"
            android:layout_row="3"
            android:layout_rowWeight="1"
            android:adjustViewBounds="false"
            android:contentDescription="@string/img_descriptor"
            app:srcCompat="@mipmap/ic_launcher" />

    </GridLayout>

</RelativeLayout>

0 个答案:

没有答案