将图像的最大高度设置为屏幕大小的比例

时间:2015-11-10 16:48:14

标签: android android-layout

我的应用中有一个屏幕,顶部有一个大图片(填充屏幕宽度),相对布局下面有一些文字和按钮。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".WelcomeScreenActivity"
>

<ImageView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:adjustViewBounds="true"
    android:id="@+id/imgFirstScreenImage"
    android:layout_centerHorizontal="true"
    android:src="@drawable/first_screen_image"/>

<TextView
    android:layout_width="200dp"
    android:layout_height="wrap_content"
    android:text="@string/first_screen_text"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:id="@+id/firstScreenText"
    android:layout_below="@+id/imgFirstScreenImage"
    android:layout_centerHorizontal="true"
    android:gravity="center" />

这款手机在Nexus 5和5X等新手机上运行良好,但在Nexus 4上,文字部分偏离屏幕底部。

我希望能够裁剪图像,使其仅填充屏幕高度的50%,以便其他元素始终显示在屏幕上,但仅限于屏幕尺寸低于一定的规模,所以我正在寻找的是某种后备。

我在drawables文件夹中使用了不同的图像分辨率,但在这种情况下似乎不够,似乎Nexus 4仍在使用xhdpi版本。我对Android很新,所以我很容易弄错,或者完全看错了方向。

1 个答案:

答案 0 :(得分:0)

如果您可以为屏幕定义一定的高度百分比,那将更实用。

喜欢 - 图像为70%,其余为30%。您可以使用layout_weight和Linear布局轻松实现此目的。

但如果你继续关注你的要求,你可以按照以下步骤进行,

  1. 以编程方式获取设备高度。
  2. 如果设备高度<一定的大小,然后以编程方式设置imageView最大高度。
  3. 前 - imageView最大高度=设备高度/ 2

    否则保持原样。

    这可能有用。