Установите максимальную ширину изображения ImageView в процентах от ее родительской ширины

Я загружаю навигацию с веб-сайта, где каждый элемент имеет заголовок, описание и изображение. Я хочу, чтобы все названия и описания были сосредоточены вдоль одной оси.

Я использую следующий макет для каждого элемента в ListView. Он отлично работает, когда изображения наполовину широки, чем они высоки, но не если они более широкие - текст идет под изображение.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_height="fill_parent"
    android:layout_width="fill_parent"
    android:orientation="horizontal">
    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">
        <LinearLayout
            android:orientation="vertical"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="1">
            <TextView
                android:layout_height="wrap_content"
                android:textSize="10pt"
                android:id="@+id/title"
                android:gravity="center_horizontal"
                android:layout_width="fill_parent" />
            <TextView
                android:layout_height="wrap_content"
                android:id="@+id/description"
                android:layout_width="fill_parent"
                android:gravity="center_horizontal" />
        </LinearLayout>
        <View
            android:layout_height="fill_parent"
            android:layout_width="fill_parent"
            android:layout_weight="3" />
    </LinearLayout>
    <ImageView
        android:id="@+id/icon"
        android:adjustViewBounds="true"
        android:layout_width="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:layout_height="wrap_content" />
</RelativeLayout>

Можно ли заставить изображение оставаться на уровне 25% от ширины RelativeLayout или меньше?

Ответы

Ответ 1

Это не может быть сделано в XML. Вы должны будете указать размер в коде. Я бы сделал это в onCreate() упражнения:

import android.widget.LinearLayout.LayoutParams;
...
obj.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
                                     LayoutParams.WRAP_CONTENT,
                                     0.25f));

Это означает использование 100% родительской высоты, но 25% родительской ширины.