如何在android中调整RelativeLayout中ImageView的高度

问题描述 投票:0回答:2

我创建了一个视图来显示包含以下内容的错误:

  • 一个图像
  • 描述文字
  • 一个“重试”按钮

必须完整显示说明文字:它显示在3行上。 “重试”按钮具有固定高度,也必须可见。必须根据可用空间调整图像大小。

目前,文本未完全显示:只能看到2行。似乎图像的大小不够减少......

此视图基于RelativeLayout,看起来像这样:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout  xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:background="@color/color_white">

    <ImageView
        android:id="@+id/ErrorImageView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:scaleType="fitCenter"
        android:adjustViewBounds="true"
        android:layout_marginTop="@dimen/cards_error_image_margin_top"
        android:layout_centerHorizontal="true"/>

    <TextView
        android:id="@+id/ErrorTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_below="@id/ErrorImageView"
        android:layout_marginHorizontal="@dimen/cards_error_text_margin_horizontal"
        android:layout_marginTop="@dimen/cards_error_text_margin_top"
        style="@style/CustomFont30Marine"
        android:gravity="center_horizontal"/>

    <Button
        android:id="@+id/RetryButton"
        android:layout_width="@dimen/cards_error_retry_button_width"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_alignParentBottom="true"
        android:layout_marginBottom="@dimen/cards_error_retry_button_margin_bottom"
        style="@style/WhiteCustomButton"/>

</RelativeLayout>

有没有办法通过设计师实现这一目标?或者我是否需要以编程方式调整图像大小?

预期结果如下所示:

Expected result

实际结果如下:

Actual result

android android-imageview scale android-relativelayout
2个回答
0
投票

尝试根据您的要求复制设计。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
>

<ImageView
    android:id="@+id/ErrorImageView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:scaleType="fitCenter"
    android:adjustViewBounds="true"
    android:layout_above="@id/ErrorTextView"
    android:background="@mipmap/ic_launcher"
    android:layout_marginTop="10dp"
    android:layout_centerHorizontal="true"/>

<TextView
    android:id="@+id/ErrorTextView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/app_name"
    android:lines="3"
    android:layout_centerHorizontal="true"
   android:layout_above="@id/RetryButton"
    android:layout_marginHorizontal="10dp"
    android:layout_marginTop="10dp"

    android:gravity="center_horizontal"/>

<Button
    android:id="@+id/RetryButton"
    android:layout_width="100dp"
    android:layout_height="50dp"
    android:layout_centerHorizontal="true"
    android:layout_alignParentBottom="true"
    android:layout_marginBottom="10dp"
    />

   </RelativeLayout>

希望这可以帮助。


0
投票

尝试

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:background="@color/color_white"
    android:gravity="center_horizontal"
    android:orientation="vertical">

    <ImageView
        android:id="@+id/ErrorImageView"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_marginTop="@dimen/cards_error_image_margin_top"
        android:layout_weight="1"
        android:adjustViewBounds="true"
        android:scaleType="fitCenter" />

    <TextView
        android:id="@+id/ErrorTextView"
        style="@style/CustomFont30Marine"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="@dimen/cards_error_text_margin_top"
        android:gravity="center_horizontal" />

    <Button
        android:id="@+id/RetryButton"
        style="@style/WhiteCustomButton"
        android:layout_width="@dimen/cards_error_retry_button_width"
        android:layout_height="wrap_content"
        android:layout_marginBottom="@dimen/cards_error_retry_button_margin_bottom" />

</LinearLayout>
© www.soinside.com 2019 - 2024. All rights reserved.