【发布时间】:2014-08-30 07:27:35
【问题描述】:
这是一个简单的布局:
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/companyIcon"
android:layout_width="wrap_content"
android:layout_height="40dp" <!-- notice I've limited a height -->
android:scaleType="fitStart"
android:adjustViewBounds="true"
android:layout_alignParentLeft="true" />
<TextView
android:id="@+id/companyName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/companyIcon"
android:layout_marginLeft="3dp"
android:layout_centerVertical="true"
android:textStyle="bold"
android:textColor="#20526d" />
</RelativeLayout>
我将由setImageBitmap() 设置的图像高度超过 40dp。
使用这种布局我在ImageView 和TextView 之间有一个额外的空间,它是从哪里来的?
但是在我用FrameLayout 包裹ImageView 之后,我就没有这个不必要的额外空间了:
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<FrameLayout
android:id="@+id/image_container"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/companyIcon"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:scaleType="fitStart"
android:adjustViewBounds="true"
android:layout_alignParentLeft="true" />
</FrameLayout>
<TextView
android:id="@+id/companyName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/image_container"
android:layout_marginLeft="3dp"
android:layout_centerVertical="true"
android:textStyle="bold"
android:textColor="#20526d" />
</RelativeLayout>
结果:
你们能解释一下为什么我要将ImageView 放入FrameLayout 以达到预期效果吗?非常感谢。
【问题讨论】:
-
您可以尝试使用 LinearLayout 而不是 Relativelayout 作为父级。很多情况下,RelativeLayout 都被证明是错误的。
-
您必须将 android:layout_width="80dp or 100dp" 分配给您的 ImageView。不要担心您的图像不会拉伸/压缩