android布局缩放,固定宽高比在不同的手机上

时间:2011-07-08 12:34:24

标签: android image-scaling

我有一个内部有两个ImageView的布局。每个图像具有固定的宽高比,例如第一图像是320x160,第二图像是320x320。布局垂直对齐。我希望将这两个图像粘合在一起并缩放以适合屏幕两侧(宽度或高度),并在另一侧进行缩放。 我尝试使用scaleType = fitCenter,但问题是在不同宽高比的手机上,图像不在一起,它们之间有黑色区域。 似乎我不能使用布局:重量,因为屏幕有不同的比例(480x854和480x800),我需要我的布局保持相同的比例。

感谢任何帮助。

这是我现在的布局:

<LinearLayout 
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<ImageView android:src="@drawable/im_menu"
 android:adjustViewBounds="true"
 android:scaleType="fitCenter"
 android:layout_width="fill_parent" android:layout_height="wrap_content"
 android:layout_gravity="center_horizontal|top"
 android:layout_weight="0.7"
>
</ImageView>
<ImageView android:src="@drawable/im_field" 
 android:adjustViewBounds="true"
 android:scaleType="fitCenter"
 android:layout_width="fill_parent" android:layout_height="wrap_content"
 android:layout_gravity="center_horizontal|top"
 android:layout_weight="0.3"
>
</ImageView>
</LinearLayout>

2 个答案:

答案 0 :(得分:6)

您可以使用:

android:adjustViewBounds="true"

并将fill_parent设置为高度,例如

<ImageView 
...
android:layout_height="fill_parent"
...>

adjustViewBounds了解我的详情

答案 1 :(得分:2)

您也可以使用相对值的布局权重。例如,如果您想要一个30%的字段和70%的其他字段,只需将子视图权重设置为0.3和0.7,并且不要在子项中指定宽度和高度。 开发人员指南(提示部分)对此进行了解释:

developer guide - linear layout

此外,您可能必须使用RelativeLayout而不是LinearLayout。

相关问题