使 ImageView 占用所有可用的垂直 space 并在 RelativeLayout 中按比例增加宽度以缩放 up/down

Make ImageView take up all available vertical space and grow width to scale up/down proportionally in RelativeLayout

我的目标是 API 等级 21,最小等级也是 21(我目前只支持 Lollipop,这是一个需要学习的宠物项目)。 我在使用自定义 ListView 项目布局时遇到问题。这是我的 XML:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                xmlns:tools="http://schemas.android.com/tools"
                android:layout_width="match_parent"
                android:layout_height="wrap_content">

    <ImageView
        android:id="@+id/test_list_item_icon"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_centerVertical="true"
        android:layout_marginEnd="10dp"
        android:background="#FF0000"
        android:src="@drawable/ic_test"
        tools:ignore="ContentDescription"/>

    <TextView
        android:id="@+id/test_list_item_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toEndOf="@id/test_list_item_icon"
        android:textSize="50sp"/>

</RelativeLayout>

这是它的样子(图标的红色背景只是为了看实际视图有多大):

我希望图像视图的行为如下所示:在布局的 'start'/左侧占用可用的垂直 space,使图像按比例缩放,并具有宽度成长以便图像可以完全显示,像这样(请原谅我的 gimp 技能):

可能吗?

为您的 ImageView

试试这个
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent">


 <ImageView
        android:id="@+id/test_list_item_icon"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_centerVertical="true"
        android:layout_marginEnd="10dp"
        android:background="#FF0000"
        android:src="@drawable/ic_test"
        android:adjustViewBounds="true"
        android:scaleType="fitXY"
        tools:ignore="ContentDescription"/>

    <TextView
        android:id="@+id/test_list_item_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toEndOf="@id/test_list_item_icon"
        android:textSize="50sp"/>

</RelativeLayout>

嗨,wujek,你的问题的原因是你将高度设置为相对布局的包装内容。所以它将把文本视图的高度作为它的高度。然后图像视图将使用它作为它的高度。所以改变相对布局高度以匹配父级。请尝试让我知道谢谢

 <?xml version="1.0" encoding="utf-8"?>
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content" >

<ImageView
    android:id="@+id/test_list_item_icon"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_centerVertical="true"
    android:layout_marginEnd="10dp"
    android:adjustViewBounds="true"
    android:background="#ff0000"
    android:src="@drawable/ic_launcher"
    tools:ignore="ContentDescription" />

<TextView
    android:id="@+id/test_list_item_name"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_toRightOf="@+id/layout_1"
    android:gravity="center"
    android:text="bhsbd"
    android:textSize="50sp" />

试试这个

如果你想要图像应该捕获 imageview 大小使用 background insted src。 如果必须使用红色背景,则使用另一个 relative layout 作为 imageview.

的父级

希望对你有用..

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >

    <RelativeLayout
        android:id="@+id/layout_1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:background="#FF0000" >

        <ImageView
            android:id="@+id/test_list_item_icon"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginEnd="10dp"
            android:background="@drawable/ic_launcher"
            android:adjustViewBounds="true"
            tools:ignore="ContentDescription" />
    </RelativeLayout>

    <TextView
        android:id="@+id/test_list_item_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@+id/layout_1"
        android:textSize="50sp" />

</RelativeLayout>

编辑 2

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >

    <RelativeLayout
        android:id="@+id/layout_1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/test_list_item_name"
        android:layout_alignTop="@+id/test_list_item_name"
        android:background="#FF0000" >

        <ImageView
            android:id="@+id/test_list_item_icon"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:src="@drawable/ic_launcher"
            android:adjustViewBounds="true"
            tools:ignore="ContentDescription" />
    </RelativeLayout>

    <TextView
        android:id="@+id/test_list_item_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="10dp"
        android:layout_centerVertical="true"
        android:layout_toRightOf="@+id/layout_1"
        android:textSize="50sp"
        android:text=" text 5>" />

</RelativeLayout>

最后我决定再创建一个图标大小,xxxhdpi,现在看起来都不错。 非常感谢@corsair992 在其中一条评论中提供有关错误的信息。

用weightsum定义一个LineatLayout。

<LinearLauyout 
----
    android:orientation="horizontal"
    android:weightSum="5">

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

<TextView
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:layout_weight="4">

</LinearLayout>

您可以根据需要调整权重。