我希望图像适合gridlayout的imageview。当我没有给imageview提供源时,布局是正确的。但是当我把光源放在那里时,imageview的宽度和高度是错误的。有人可以帮我解决问题吗?我这是一场比赛。
没有src的XML
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.ktappworks.test.MainActivity">
<GridLayout
android:id="@+id/ph0"
android:layout_width="300dp"
android:layout_height="300dp"
android:layout_columnWeight="1"
android:layout_gravity="fill"
android:layout_marginTop="0dp"
android:clipChildren="false"
android:columnCount="5"
android:rowCount="2"
android:src="@mipmap/ic_launcher"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginRight="8dp"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
android:layout_marginBottom="8dp"
android:layout_marginLeft="8dp"
app:layout_constraintLeft_toLeftOf="parent">
<ImageView
android:id="@+id/ph00"
android:layout_columnWeight="1"
android:layout_margin="2dp"
android:layout_rowWeight="1"
/>
<ImageView
android:id="@+id/ph01"
android:layout_columnWeight="1"
android:layout_margin="2dp"
android:layout_rowWeight="1"
/>
<ImageView
android:id="@+id/ph02"
android:layout_columnWeight="1"
android:layout_margin="2dp"
android:layout_rowWeight="1"
/>
<ImageView
android:id="@+id/ph03"
android:layout_columnWeight="1"
android:layout_margin="2dp"
android:layout_rowWeight="1"
/>
<ImageView
android:id="@+id/ph04"
android:layout_columnWeight="1"
android:layout_margin="2dp"
android:layout_rowWeight="1"
/>
<ImageView
android:id="@+id/ph05"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_columnWeight="1"
android:layout_margin="2dp"
android:layout_rowWeight="1"
/>
<ImageView
android:id="@+id/ph06"
android:layout_columnWeight="1"
android:layout_margin="2dp"
android:layout_rowWeight="1"
/>
<ImageView
android:id="@+id/ph07"
android:layout_columnWeight="1"
android:layout_margin="2dp"
android:layout_rowWeight="1"
/>
<ImageView
android:id="@+id/ph08"
android:layout_columnWeight="1"
android:layout_margin="2dp"
android:layout_rowWeight="1"
/>
<ImageView
android:id="@+id/ph09"
android:layout_columnWeight="1"
android:layout_margin="2dp"
android:layout_rowWeight="1"
/>
</GridLayout>
</android.support.constraint.ConstraintLayout>
带有src的XML
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.ktappworks.test.MainActivity">
<GridLayout
android:id="@+id/ph0"
android:layout_width="300dp"
android:layout_height="300dp"
android:layout_columnWeight="1"
android:layout_gravity="fill"
android:layout_marginTop="0dp"
android:clipChildren="false"
android:columnCount="5"
android:rowCount="2"
android:src="@mipmap/ic_launcher"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginRight="8dp"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
android:layout_marginBottom="8dp"
android:layout_marginLeft="8dp"
app:layout_constraintLeft_toLeftOf="parent">
<ImageView
android:id="@+id/ph00"
android:layout_columnWeight="1"
android:layout_margin="2dp"
android:layout_rowWeight="1"
android:scaleType="fitXY"
android:src="@drawable/owl" />
<ImageView
android:id="@+id/ph01"
android:layout_columnWeight="1"
android:layout_margin="2dp"
android:layout_rowWeight="1"
android:scaleType="fitXY"
android:src="@drawable/owl" />
<ImageView
android:id="@+id/ph02"
android:layout_columnWeight="1"
android:layout_margin="2dp"
android:layout_rowWeight="1"
android:scaleType="fitXY"
android:src="@drawable/owl" />
<ImageView
android:id="@+id/ph03"
android:layout_columnWeight="1"
android:layout_margin="2dp"
android:layout_rowWeight="1"
android:scaleType="fitXY"
android:src="@drawable/owl" />
<ImageView
android:id="@+id/ph04"
android:layout_columnWeight="1"
android:layout_margin="2dp"
android:layout_rowWeight="1"
android:scaleType="fitXY"
android:src="@drawable/owl" />
<ImageView
android:id="@+id/ph05"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_columnWeight="1"
android:layout_margin="2dp"
android:layout_rowWeight="1"
android:scaleType="fitXY"
android:src="@drawable/owl" />
<ImageView
android:id="@+id/ph06"
android:layout_columnWeight="1"
android:layout_margin="2dp"
android:layout_rowWeight="1"
android:scaleType="fitXY"
android:src="@drawable/owl" />
<ImageView
android:id="@+id/ph07"
android:layout_columnWeight="1"
android:layout_margin="2dp"
android:layout_rowWeight="1"
android:scaleType="fitXY"
android:src="@drawable/owl" />
<ImageView
android:id="@+id/ph08"
android:layout_columnWeight="1"
android:layout_margin="2dp"
android:layout_rowWeight="1"
android:scaleType="fitXY"
android:src="@drawable/owl" />
<ImageView
android:id="@+id/ph09"
android:layout_columnWeight="1"
android:layout_margin="2dp"
android:layout_rowWeight="1"
android:scaleType="fitXY"
android:src="@drawable/owl" />
</GridLayout>
</android.support.constraint.ConstraintLayout>
答案 0 :(得分:0)
您需要将每个ImageView
的宽度和高度定义为0dp
,以使列/行权重生效。我认为默认值为wrap_content
。您可能还想将scaleType
更改为fitXY
以外的其他内容,因为fitXY
会扭曲图像。
在使用某些API级别的权重时,我也遇到了GridLayout
的本机实现问题。 (我最近回答了这样一个问题,如果我能找到它,我会在这里发布一个引用。)你可能想考虑使用GridLayout
的支持库版本,因为它似乎有一个有效的权重实现。将以下内容添加到此版本GridLayout
的gradle文件中:
compile 'com.android.support:gridlayout-v7:26.1.0'
以下是进行这些更改后的屏幕截图: