我有这样的布局:
<?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="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_toLeftOf="@+id/my_image"
android:ellipsize="end"
android:singleLine="true"
android:text="Some text"
android:textAppearance="?android:attr/textAppearanceMedium" />
<ImageView
android:id="@+id/my_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/title"
android:layout_alignBottom="@+id/title"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:adjustViewBounds="true"
android:src="@drawable/my_bitmap_image" />
Run Code Online (Sandbox Code Playgroud)
这种布局几乎满足了我的需要:它使图像视图高度与文本视图相同。图像图形内容拉伸也保持纵横比。
但是,图像视图的宽度不会改变!结果,文本和图像视图之间存在很大差距!作为临时解决方案,我覆盖了View#onLayout
.
问题:如何更改 xml 布局中的图像宽度?
更新:
这是我需要的最终布局(文本+一些图像)。看第一张图像:它的宽度应该与其中缩放后的图像完全相同,没有填充和边距: