简体   繁体   English

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

[英]How to adjust height's of ImageView in RelativeLayout in android

I've created an view to display errors that contains: 我创建了一个视图来显示包含以下内容的错误:

  • an image 图片
  • a description text 说明文字
  • a "Retry" button 一个“重试” 按钮

The description text must be fully displayed: it is displayed on 3 lines. 说明文字必须完整显示:以3行显示。 The "Retry" button has a fixed height and must be visible too. “重试” 按钮具有固定的高度,也必须可见。 The image must be resized according to the available space. 必须根据可用空间调整图像大小。

For now, the text is not fully displayed: only 2 lines are visible. 目前,该文本尚未完全显示:仅可见两行。 It seems that the image's size is not enough reduced... 图像的大小似乎还不够缩小...

This view is based on a RelativeLayout , and looks like this: 该视图基于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>

Is there a way to achieve this through the designer? 有没有办法通过设计师实现这一目标? Or do I need to resize the image programmatically? 还是需要以编程方式调整图像大小?

The expected result looks like this: 预期结果如下所示:

预期结果

The actual result looks like this: 实际结果如下所示:

实际结果

Have tried to replicate the design as per your requirements . 尝试根据您的要求复制设计。

<?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>

Hope this helps. 希望这可以帮助。

try 尝试

<?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>

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

相关问题 如何在Android上显示RelativeLayout和ImageView? - How to display RelativeLayout and ImageView on Android? 如何使ImageView的高度成为图像的高度? Android的 - How to make an ImageView's height be the image's height? Android Android - 如何使子LinearLayout与父RelativeLayout的高度匹配 - Android - How to make child LinearLayout match parent RelativeLayout's height RelativeLayout Imageview android:layout_height =“ wrap_content”未唤醒 - RelativeLayout Imageview android:layout_height=“wrap_content” not woking Android如何从RelativeLayout移除ImageView - Android How to Remove ImageView from RelativeLayout Android,如何在relativelayout中设置具有百分比的imageview - Android, how to set imageview with percentage in relativelayout 如何在relativelayout android中平均分配imageview间距? - How to allign imageview spacing equally in relativelayout android? 在Android中将ImageView添加到RelativeLayout - Add ImageView to RelativeLayout in Android 在PagerAdapter中将RelativeLayout包装器调整为ImageView的大小 - Adjust RelativeLayout wrapper to size of ImageView in a PagerAdapter 如何以编程方式调整 android 中选项卡的高度和加粗文本? - How to adjust the height and bold the text of the tab's in android programmatically?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM