如何将RelativeLayout适配屏幕?

5
我是一名有用的助手,会翻译文本。
我遇到了一个问题,按钮在RelativeLayout中显示在屏幕外。 ImageView没有缩放图片以使按钮有可见位置。
我的情况如下:

enter image description here

我想要什么:

enter image description here

代码:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:gravity="center_vertical"  >

<TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/str"
        android:textAppearance="@android:style/TextAppearance.Small"
        android:textColor="?android:attr/textColorTertiary"
        android:layout_centerHorizontal="true"
        />

<ru.mw.widget.DrawableImageView
        android:id="@+id/imageView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView"
        android:adjustViewBounds="true"
        android:layout_centerHorizontal="true"
        android:scaleType="centerCrop"
        />

<Button
        android:id="@+id/processButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/str"
        android:layout_below="@+id/imageView"
        android:layout_centerHorizontal="true"
        />

当我更改屏幕方向时出现问题: 如果我在横屏模式下使用Arun C Thomas的方法,一切正常,但在竖屏模式下,我会看到这个(图像被左/右边缘裁剪): enter image description here 预期结果是什么:

enter image description here


为你的按钮添加android:alignParentBottom属性,为TextView添加android:alignParentTop属性(两个值都设置为true)。目前你还没有为ImageView设置任何边界。 - Stefan de Bruijn
3个回答

2
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:gravity="center_vertical"  >

<TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/str"
        android:textColor="?android:attr/textColorTertiary"
        android:textAppearance="@android:style/TextAppearance.Small"

        android:layout_centerHorizontal="true"
        />

<ru.mw.widget.DrawableImageView
    android:id="@+id/imageView"
    android:layout_width="wrap_content"
    android:layout_height="0dp"
    android:layout_below="@+id/textView"
    android:layout_centerHorizontal="true"
    android:adjustViewBounds="true"
    android:scaleType="centerCrop"
    android:layout_above="@+id/processButton"
     />

<Button
        android:id="@+id/processButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/str"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        />
</RelativeLayout>

这是解决方案。

现在,为了实现更新的要求,请将上述布局添加到res文件夹中的layout-land文件夹中(如果没有,请创建一个名为layout-land的文件夹),然后在默认的layout文件夹中添加此xml。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:gravity="center"  >
<ru.mw.widget.DrawableImageView
    android:id="@+id/imageView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    android:adjustViewBounds="true"
     />

<Button
    android:id="@+id/processButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/imageView"
    android:layout_centerHorizontal="true"
    android:text="@string/str" />

<TextView
    android:id="@+id/textView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/imageView"
    android:layout_centerHorizontal="true"
    android:text="@string/str"
    android:textColor="?android:attr/textColorTertiary" />

</RelativeLayout>

这就是它。


我希望这可以通过一个布局实现,但你是正确的,谢谢! - ADK

1

尝试将android:layout_alignParentBottom="true"添加到按钮中:

<Button
    android:id="@+id/processButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/str"
    android:layout_below="@+id/imageView"
    android:layout_centerHorizontal="true"
    android:layout_alignParentBottom="true"
    />

1
  1. 使用layout_alignParentBottom="true"将按钮与父布局底部对齐。
  2. ImageView包裹在TextViewButton之间。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:gravity="center_vertical"  >

<TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/str"
        android:textAppearance="@android:style/TextAppearance.Small"
        android:textColor="?android:attr/textColorTertiary"
        android:layout_centerHorizontal="true"
        />

<Button
        android:id="@+id/processButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/str"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        />

<ru.mw.widget.DrawableImageView
        android:id="@+id/imageView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView"
        android:layout_above="@+id/processButton"
        android:adjustViewBounds="true"
        android:layout_centerHorizontal="true"
        android:scaleType="centerCrop"
        />

</RelativeLayout>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接