【问题标题】:How to avoid view overlapping in RelativeLayout如何避免相对布局中的视图重叠
【发布时间】:2017-03-27 14:03:27
【问题描述】:

我的布局结构如下:

<RelativeLayout>
    <ScrollView>
        <LinearLayout>
            <RelativeLayout>
                <LinearLayout android:id="@+id/ContentNo1"> <-- Align top
                </LinearLayout>
                <LinearLayout android:id="@+id/ContentNo2"> <-- Align bottom
                </LinearLayout>
            </RelativeLayout>
        </LinearLayout>
    </ScrollView>
    <Button/> <-- Always bottom of the page
</RelativeLayout>

我的按钮在 FIRST RelativeLayout 中是底部对齐的,而我的 ScrollView 在其中是顶部对齐的...

我想在第二个RelativeLayout中顶部对齐ContentNo1,并在其中底部对齐ContentNo2...

我已经这样做了,但是当 ContentNo1 变得太大时,它会与 ContentNo2 重叠,我希望它只是将 ContentNo2 向下推...如何做到这一点?

我已经尝试(如这里的一些主题中所述)使用 layout_below / layout_above,但是当我使用它时,ContentNo2 的底部对齐被解除...

-- 编辑--

按照Wasi的要求,这里是图片:(我不能发布超过2个链接,然后我制作了这个页面来解释它)

http://www.mydonorlife.hol.es/relativeissue/

-- 编辑 2 -- 解决方案 --

这对我有用:我删除了第二个 RelativeLayout 并在某些视图上设置了一些重量/高度技巧,如下所示 >>

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:layout_alignParentTop="true"
        android:fillViewport="true">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">
                                                   <!-- CONTENT NO 1 -->
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical"
                android:layout_marginBottom="30dp">
            </LinearLayout>
                                                   <!-- /CONTENT NO 1 -->
            <LinearLayout
                android:layout_width="match_parent"
                android:orientation="vertical"
                android:layout_weight="1"
                android:layout_height="0dp">
                                                   <!-- CONTENT NO 2 -->
                <LinearLayout
                    android:orientation="vertical"
                    android:layout_width="match_parent"
                    android:gravity="bottom"
                    android:layout_height="match_parent"
                    android:layout_weight="0">
                </LinearLayout>
                                                   <!-- /CONTENT NO 2 -->
            </LinearLayout>
        </LinearLayout>
    </ScrollView>
    <Button
        android:layout_alignParentBottom="true"/>
</RelativeLayout>

希望以后对大家有所帮助...

【问题讨论】:

  • 您可以使用匹配父级到外部 LinearLayout 使其全屏,然后使用 layout_below / layout_above。然后底部对齐不会消失。因为,使用 layout_below / layout_above 可以确保两个相对布局之间没有重叠。
  • 外部 LinearLayout 已经设置了 match_parent 高度,当我使用 layout_below / above 时,底部对齐不断消失
  • 底部对齐消失意味着 content2 不适合屏幕的其余部分?你能分享一张照片吗?一张图片可能会更好地描述这个问题!
  • 我已将它们添加到问题中

标签: android xml android-layout layout


【解决方案1】:

我已经更改了您的一些代码并尝试根据您的需要进行布局。

 <RelativeLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
        <ScrollView
            android:id="@+id/scrollview"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:layout_alignParentTop="true"
            android:fillViewport="true">
            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical">
                <!-- CONTENT NO 1 -->
                <LinearLayout
                    android:id="@+id/linerlayout"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:orientation="vertical"
                    android:layout_marginBottom="30dp">
                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="Top aligned text1"/>
                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="Top aligned text2"/>
                </LinearLayout>
                <!-- /CONTENT NO 1 -->
                <LinearLayout
                    android:layout_width="match_parent"
                    android:orientation="vertical"
                    android:layout_below="@+id/linerlayout"
                    android:layout_height="wrap_content">
                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="bottom aligned text1"/>
                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="bottom aligned text2"/>
                    <!-- CONTENT NO 2 -->
                    <LinearLayout
                        android:orientation="vertical"
                        android:layout_width="match_parent"
                        android:gravity="bottom"
                        android:layout_height="match_parent"
                        android:layout_weight="0">
                    </LinearLayout>
                    <!-- /CONTENT NO 2 -->
                </LinearLayout>
            </RelativeLayout>
        </ScrollView>
        <Button
            android:layout_alignBottom="@id/scrollview"
            android:text="bottom button"
            android:layout_alignParentBottom="true"
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            />
    </RelativeLayout>

您还可以根据需要添加 CONTENT NO 1CONTENT NO 2 用户界面

【讨论】:

  • @T.Lima 谢谢。
猜你喜欢
  • 2021-12-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多