繁体   English   中英

android 在不同手机上具有固定纵横比的布局缩放

[英]android layout scaling with fixed aspect ratio on different phones

我有一个布局,里面有两个 ImageView。 每张图片都有固定的纵横比,例如第一张图片是 320x160,第二张是 320x320。 布局垂直对齐。 我希望将这两个图像粘合在一起并缩放以适合屏幕的一侧(宽度或高度)并按比例缩放另一侧。 我尝试使用scaleType=fitCenter,但问题是在不同长宽比的不同手机上,图像不在一起,它们之间有一个黑色区域。 似乎我不能使用 layout:weight 因为屏幕有不同的比例(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>

您可以使用:

android:adjustViewBounds="true"

并将fill_parent设置为 height 例如

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

adjustViewBounds了解我的详细信息

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

开发者指南 - 线性布局

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM