首页 > 解决方案 > Android:Imageview在上面对象的高度被改变时被调整大小

问题描述

这是我的布局 XML

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.homerevise.studyapp.activity.HomeScreenActivity"
    tools:showIn= "@layout/app_bar_main">

    <RelativeLayout
        android:id="@+id/rel_colour"
        android:layout_width="match_parent"
        android:layout_height="250dp"
        android:background="@drawable/path_background">

        <ImageView
            android:id="@+id/image_more"
            android:layout_width="25dp"
            android:layout_height="25dp"
            android:layout_marginLeft="20dp"
            android:layout_marginTop="25dp"
            android:background="@drawable/more"/>

        <TextView
            android:id="@id/txtName"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="20dp"
            android:layout_marginTop="90dp"
            android:textColor="#000"
            android:textSize="24sp"
            android:visibility="invisible" />

        <ImageView
            android:id="@+id/image_page_title"
            android:layout_width="240dp"
            android:layout_height="120dp"
            android:layout_below="@id/txtName"
            android:layout_marginLeft="20dp"
            android:layout_marginTop="20dp"
            android:background="@drawable/page_title" />

        <ImageView
            android:id="@+id/image_happy_student"
            android:layout_width="120dp"
            android:layout_height="165dp"
            android:layout_alignEnd="@id/image_page_title"
            android:layout_alignRight="@id/image_page_title"
            android:layout_alignParentRight="true"
            android:layout_marginTop="75dp"
            android:layout_marginRight="20dp"
            android:background="@drawable/happy_student" />
    </RelativeLayout>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <ImageView
            android:id="@+id/image_subject_title"
            android:layout_width="80dp"
            android:layout_height="30dp"
            android:layout_marginLeft="20dp"
            android:layout_marginTop="10dp"
            android:background="@drawable/subject_title" />

        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/recyclerView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="20dp"
            android:layout_marginTop="10dp"
            android:layout_below="@id/image_subject_title"
            android:orientation="horizontal" />

        <ProgressBar
            android:id="@+id/progressBar"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_centerVertical="true"
            android:visibility="gone" />

        <TextView
            android:id="@+id/txtRecentlyLearned"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="20dp"
            android:layout_marginTop="10dp"
            android:layout_marginRight="20dp"
            android:layout_below="@id/recyclerView"
            android:text="Recently Learned"
            android:textColor="#2A3E61"
            android:textSize="24sp"
            android:textStyle="bold" />

        <ImageView
            android:id="@+id/image_Recently_Learned"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@id/txtRecentlyLearned"
            android:layout_marginLeft="10dp"
            android:layout_marginTop="10dp"
            android:layout_marginRight="10dp"
            android:src="@drawable/recently_learned"
            android:layout_marginBottom="90dp"/>
    
        </RelativeLayout>
    
    </LinearLayout>

如果回收站视图的布局高度发生变化,图像视图 txtRecentlyLearned 将被重新占用。它变得太小了。就像即使回收站视图的高度更改为 250dp,图像大小也会大幅下降。Imageview 根本不应该调整大小。

我该如何解决?

谢谢你。

标签: androidandroid-studioandroid-layout

解决方案


将RelativeLayout外的textView和imageView移动到父LinearLayout并添加android:layout_weight="1"到RelativeLayout

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_weight="1"
        android:layout_height="0dp">

        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/recyclerView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

    </RelativeLayout>

    <TextView
        android:id="@+id/txtRecentlyLearned"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

    <ImageView
        android:id="@+id/image_Recently_Learned"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:src="@drawable/recently_learned" />

</LinearLayout>

推荐阅读