У меня есть GridLayout, как это
<android.support.v7.widget.GridLayout
android:id="@+id/itemContainer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:columnCount="3"
>
</android.support.v7.widget.GridLayout>
во время выполнения я заполняю этот макет элементами, содержащими ImageViews.
Элементы выглядят так:
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/__item_background"
android:padding="15dp"
android:layout_margin="2dp">
<ImageView
android:id="@+id/image_view"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
android:scaleType="fitCenter"
app:layout_constraintBottom_toTopOf="@+id/itemText"
app:layout_constraintDimensionRatio="1:1"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:src="@drawable/placeholder"/>
<TextView
android:id="@+id/itemText"
android:layout_height="30sp"
android:layout_width="match_parent"
app:layout_constrainedWidth="true"
app:autoSizeTextType="uniform"
android:gravity="center"
android:layout_gravity="fill"
android:layout_marginStart="8dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="8dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/image_view"
/>
</android.support.constraint.ConstraintLayout>
А вот код Java
View itemLayout = View.inflate(this, R.layout.task_item, null);
itemLayout.setLayoutParams( new GridLayout.LayoutParams(GridLayout.spec(GridLayout.UNDEFINED, 1f), GridLayout.spec(GridLayout.UNDEFINED, 1f)));
ImageView imageView = itemLayout.findViewById(R.id.image_view);
int img = getResources().getIdentifier(dynamicImage, "drawable", getPackageName());
if (img != 0)
imageView.setImageResource(img);
itemContainer.addView(itemLayout); //Gridlayout @+id/itemContainer
Я хочу, чтобы три столбца макета шлифовки были одинаковыми по ширине, но изображения масштабированы и превышают область просмотра.
Когда я предоставляю фиксированную ширину / высоту
//itemLayout.setLayoutParams( new GridLayout.LayoutParams(GridLayout.spec(GridLayout.UNDEFINED, 1f), GridLayout.spec(GridLayout.UNDEFINED, 1f)));
itemLayout.setLayoutParams(new ConstraintLayout.LayoutParams(320, 450));
все работает просто отлично.
Это также работает, если imageResource - мой вектор-заполнитель, который можно рисовать, в отличие от моих обычных изображений .png. Вот рабочий скриншот:
Пока это испорченный экран:
Очевидно, что проблема связана с масштабированием растровых изображений.
Соответствующие атрибуты, кажется,
android:scaleType="fitCenter"
и app:layout_constraintDimensionRatio="1:1"
но я понятия не имею, как их правильно изменить.