【问题标题】:Contents of ScrollView leaking out of the parent view in AndroidScrollView 的内容从 Android 的父视图中泄露出来
【发布时间】:2017-06-11 12:16:40
【问题描述】:

下面的布局文件应该给你一个想法,我想要 TextView 下方的 RelativeLayout 内的滚动视图的内容,但是 ScrollView 的内容走出 RelativeLayout 隐藏在 TextView 后面。我不明白这是怎么回事。在 ScrollView 内的 RelativeLayout 中,我以编程方式添加了一个 LinearLayoutCompat 对象,并且我还以编程方式向该对象添加了几个 AppCompatTextView 对象。屏幕截图只是布局的 1 个实例。

我将嵌套的 RelativeLayout 设置为从 TextView 下方开始,但它仍然从最顶部开始。

layout.xml

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

    <TextView
        android:id="@+id/narrowTextnon"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:background="@color/cherryRed"
        android:elevation="8dp"
        android:gravity="center"
        android:layout_marginTop="15dp"
        android:padding="25dp"
        android:text="Let's narrow down now !"
        android:textColor="@color/wineBrown"
        android:textSize="22sp" />

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/narrowTextnon"
        android:gravity="center">

        <ScrollView
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <RelativeLayout
                android:id="@+id/dataContnon"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="center">

            </RelativeLayout>

        </ScrollView>

    </RelativeLayout>

</RelativeLayout>

【问题讨论】:

    标签: android android-layout scrollview android-scrollview


    【解决方案1】:

    RelativeLayout 的正常行为,我要做的是将根布局更改为RelativeLayout,并在嵌套RelativeLayout 中声明android:layout_below="@id/narrowText"

    【讨论】:

    • 好吧,您可能会将我的答案与@Robert K. 的答案结合起来,您最终会将根布局更改为RelativeLayout,设置android:layout_below="@id/narrowText" 并将嵌套的RelativeLayout 高度更改为wrap_content
    • 我将返回几步,但这次包括将根布局更改为“RelativeLayout”以及将其设置为 android:layout_below="@id/narrowText" 的解决方案。我将保留一张我现在所拥有的屏幕截图。
    【解决方案2】:

    我猜你的问题出在这一行: android:layout_height="match_parent" 这里:&lt;RelativeLayout //To position ScrollView in center

    在那里试试wrap_content

    【讨论】:

    • 如果我这样做,我的 ScrollContent 内容将被推到顶部。我希望他们居中
    【解决方案3】:

    终于得到了我想要的应用:
    android:layout_height="match_parent" 到嵌套的 RelativeLayout 以将 ScrollView 定位在中心
    android:layout_below="@id/narrowText" 让 ScrollView 从 TextView 下方而不是最顶部开始。
    android:layout_alignParentBottom = true 让 ScrollView 从 TextView 底部伸展到屏幕底部。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-06-14
      • 1970-01-01
      • 2021-02-18
      • 1970-01-01
      • 2015-03-10
      • 1970-01-01
      • 2022-01-17
      • 1970-01-01
      相关资源
      最近更新 更多