ScrollView下方的LinearLayout未显示出来。

6
以下应该显示一个占据整个屏幕的滚动视图,除了底部有足够小的空间容纳按钮。但运行代码后,按钮和布局都没有出现,只有占据整个屏幕并包含在滚动视图中的其他视图。您有什么想法吗?
<LinearLayout android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.th3ramr0d.armybodyfatcalculator.MainActivity"
android:orientation="vertical">

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

    </ScrollView>

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

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button 1"
        />


    </LinearLayout>
</LinearLayout

这个布局可以工作,但你需要为你的滚动视图定义最大高度。 - dex
我希望设备根据其下方布局的大小为我确定滚动视图的大小。这是否可能? - th3ramr0d
然后请考虑下面@Tiago Oliveira的回答。 - dex
1个回答

5
这将为您完成工作。
<RelativeLayout android:layout_width="match_parent"
     android:layout_height="match_parent"
     xmlns:android="http://schemas.android.com/apk/res/android"
     tools:context="com.th3ramr0d.armybodyfatcalculator.MainActivity"
     xmlns:tools="http://schemas.android.com/tools"
     >
     <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentTop="true"
        android:layout_above="@+id/myButton">
     </ScrollView>

     <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button 1"
        android:layout_alignParentBottom="true"
        android:id="@+id/myButton" />
</RelativeLayout>

1
更新了,我从来没有使用RelativeLayout作为根布局时遇到过问题。 - Tiago Oliveira
@dex,你能否提供这个建议的来源?嵌套布局也会带来性能开销,因此通常建议尽可能保持布局的扁平化 - 这正是ConstraintLayout试图实现的。 - Egor
@dex 谢谢,我会看一下。 - Egor
2
@dex Chet 绝对是正确的,但我认为主要的收获是 1) 应该避免嵌套 RelativeLayouts 和 2) 当可以避免使用 RelativeLayout 时,不应该使用它。在 OP 的情况下,RelativeLayout 是一个好的解决方案,并且证明了它的使用是合理的,此外,它只有两个子元素,所以布局不会非常昂贵。将其包装在 LinearLayout 中肯定不会使它更快。 - Egor
@th3ramr0d,在你看这个的时候,可以顺便看一下ConstraintLayout,这里有一个很棒的Codelab可以尝试:https://codelabs.developers.google.com/codelabs/constraint-layout/index.html?index=..%2F..%2Findex#0 - Egor
显示剩余5条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接