Android 使imageview调整大小以适合其容器
我使用相对布局在listlayout中显示一行。我现在展示的是左边的一个图像视图和右边的两个文本视图。两个文本视图都只有一行 我需要有这样的图像,它应该自动调整大小,使其高度成为相等的文字高度组合。这样,图像高度将适合所有屏幕中两个文本的组合高度。这可能吗?布局如下图所示-Android 使imageview调整大小以适合其容器,android,android-layout,resize,imageview,android-relativelayout,Android,Android Layout,Resize,Imageview,Android Relativelayout,我使用相对布局在listlayout中显示一行。我现在展示的是左边的一个图像视图和右边的两个文本视图。两个文本视图都只有一行 我需要有这样的图像,它应该自动调整大小,使其高度成为相等的文字高度组合。这样,图像高度将适合所有屏幕中两个文本的组合高度。这可能吗?布局如下图所示- <ImageView android:id="@+id/note_img" android:layout_width="wrap_content" android:layout_height="
<ImageView
android:id="@+id/note_img"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentBottom="true"
android:layout_marginRight="6dip"
android:src="@drawable/note_icon" />
<TextView android:id="@+id/text1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/note_img"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:includeFontPadding="true"
android:singleLine="true"
style="@android:style/TextAppearance.Medium" />
<TextView android:id="@+id/text2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/note_img"
android:layout_alignParentBottom="true"
android:layout_below="@id/text1"
android:singleLine="true"
style="@android:style/TextAppearance.Small" />
到目前为止,请注意,img的大小为96x96,图像以全尺寸显示,这使得图像区域相当大,两个文本之间的间隙很大。
非常感谢您的帮助
谢谢,
Arun在
TextView
中设置Text
后,将TextView的高度设置为ImageView
类似
imageview.getLayoutParams().height = textViewheight;
不要使用fill\u parent,它已被弃用。未经测试,请告知是否有效:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<ImageView
android:id="@+id/note_img"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginRight="6dip"
android:src="@drawable/note_icon" />
<LinearLayout
android:id="@+id/vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="@+id/text1"
style="@android:style/TextAppearance.Medium"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:includeFontPadding="true"
android:singleLine="true" />
<TextView
android:id="@+id/text2"
style="@android:style/TextAppearance.Small"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:singleLine="true" />
</LinearLayout>
谢谢@svenoaks,调整大小很好,但现在图像两侧的空间太大了。这是一张图片-问题出在android上:在ImageView上的layout_width=“wrap_content”,用硬编码的dp调整它,你就可以去掉空间了。不过,尝试着想一些更好的东西。谢谢,但这会再次让它在不同的屏幕上看起来有所不同:(我认为在这种情况下,你别无选择,只能在Java中事后再做,因为没有办法提前知道ImageView的宽度。我已经成功地使用了,尽管有打字错误,他忘记了将dp转换为px或其他东西。你还必须使用ViewTreeObserver等待一切就绪。)ut获取垂直线性布局的测量值。我希望被证明是错误的,但我认为纯XML不可能正确。这是一个非常有用的链接@svenoaks。在经过广泛的谷歌搜索后,它声称为stackoverflow:)。我想我会使用我以前的代码,为mdpi/hdpi/xhdpi使用不同大小的图像。谢谢所有的帮助。谢谢@Homo Sapients,但我想在布局xml本身中这样做。另外,还有两个文本元素。因此需要一个非计算方法:)无论如何,谢谢你的帮助。