首页 > 解决方案 > 如何防止relativeLayout随着linearLayout上升

问题描述

我需要将图标保留在屏幕的底角

单击图标时,会显示一个RelativeLayout,但图标会随之上升

印刷

好的

在此处输入图像描述

问题:

在此处输入图像描述

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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">

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">

        <ImageView
            android:id="@+id/icon"
            android:layout_width="45dp"
            android:layout_height="45dp"
            android:src="@mipmap/ic_launcher"
            tools:ignore="ContentDescription" />

        <ImageView
            android:id="@+id/close"
            android:layout_width="18dp"
            android:layout_height="18dp"
            android:layout_marginStart="35dp"
            android:src="@drawable/ic_close"
            tools:ignore="ContentDescription" />
    </RelativeLayout>

    <LinearLayout
        android:id="@+id/content"
        android:layout_width="400dp"
        android:layout_height="250dp"
        android:layout_marginStart="50dp"
        android:background="#000000"
        android:orientation="vertical"
        android:visibility="gone">

        <!-- TODO -->

    </LinearLayout>
</FrameLayout>

标签: androidandroid-linearlayoutandroid-relativelayout

解决方案


您有一个FrameLayout包含 aRelativeLayout和的父级LinearLayout。AFrameLayout只是将布局放在彼此的顶部,尽管子布局可以使用layout_gravity. 一般来说,当你混合RelativeLayout和时它会变得有点疯狂LinearLayout,尽管你可以做到,只要你了解它们将如何协同工作。

有几种方法可以使您的子布局在您的问题中按照您想要的方式运行,但为了使其快速简单,请使用 parentRelativeLayout而不是FrameLayout,并让子视图将自己定位在父布局中,其中之一layout_alignXYZ属性。(见:https ://developer.android.com/reference/android/widget/RelativeLayout.LayoutParams )我希望这会有所帮助。


推荐阅读