ScrollView 和 LinearLayout

3
我正在尝试在Android中获得一个非常简单的布局,但我就是不能让它工作。我想要的只是一个标题(通过include一个XML文件),接着是一个ScrollView来显示大量文本内容,并且在底部有两个按钮,应该始终可见。
我已经尝试过使用LinearLayoutRelativeLayout,但不知何故,我无法让它正常工作。到目前为止,我所拥有的是这个:
<?xml version="1.0" encoding="utf-8"?>
 <LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent">

    <include layout="@layout/header" />


    <RelativeLayout 
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        >
    <ScrollView
        android:layout_height="wrap_content"
        android:layout_width="fill_parent"
        android:id="@+id/svDisclaimer"
        >
           <TextView 
               android:layout_width="fill_parent"
               android:layout_height="wrap_content" 
               android:id="@+id/tvDisclaimer">
           </TextView>
    </ScrollView>
         <LinearLayout      android:orientation="horizontal" 
              android:layout_width="fill_parent" 
              android:layout_height="wrap_content" 
              android:layout_below="@+id/svDisclaimer"
               >
      .. [ snip ] ..
        </LinearLayout>
        </RelativeLayout>
</LinearLayout>

(..[snip].. 是我的按钮所在的位置) 头部显示正常,滚动视图也弹出了,但是按钮却不见了。

3个回答

4

你的ScrollView的高度被设置为fill_parent,这将会把ScrollView下方的所有内容都推到屏幕底部,而你永远看不到那些无法滚动的内容... 请尝试使用以下方式:

<ScrollView >
    <RelativeLayout >
        <TextView />
        <LinearLayout >
            <!-- [ snip ] -->
        </LinearLayout>
    </RelativeLayout>
</ScrollView>

您可以使用RelativeLayout的位置标签(例如,android:below,android:toRightOf等)来删除LinearLayout。尝试使用我评论中的此配置:
<ScrollView
    ...
    android:above="@+id/buttons" >

    ...
</ScrollView>
<LinearLayout
    android:id="@+id/buttons"
    android:layout_alignParentBottom="true"
    ... >

    ...
</LinearLayout>

这是我的两个按钮。我希望它们始终显示在屏幕上,所以要放在滚动视图之外。我尝试将滚动视图的高度设置为wrap_content,但没有成功。 - Bart Friederichs
好的,使用 android:layout_alignParentBottom 将您的按钮与屏幕底部对齐以将它们固定在屏幕底部。 - Sam
尝试将其放入“LinearLayout”中,但无效。 - Bart Friederichs
关于你最后的评论:添加layout_above会使应用程序在Choregrapher(无论那是什么)中崩溃。 - Bart Friederichs
我好像明白了。同时使用layout_belowlayout_above会导致崩溃。奇怪的是其中一个可以工作,而另一个却不行。显然这与将height设置为fill_parent有关。当我理解Android布局时,这将是美好的一天:)。感谢您的帮助! - Bart Friederichs
对不起,我在头部删除了below属性,但我并没有明确告诉你删除它。在Android中同时使用abovebelow会导致循环逻辑...很高兴我能帮到你! - Sam

0
像Sam说的那样,将ScrollView的高度设置为fill_parent会将按钮推出屏幕。 然而,使用RelativeLayout的布局标签有一种使其正常工作的方法。 请尝试以下内容:
<LinearLayout>
[snip]
    <RelativeLayout>
         <!-- Declare the buttons *before* the ScrollView, but use layout params to move
         them to the bottom of the screen -->
         <LinearLayout
            android:id="@+id/buttonParent"
            android:layout_alignParentBottom="true">
              [Buttons go here]
         </LinearLayout>
         <!-- The "android:layout_above" attribute forces the ScrollView to leave space for the buttons -->
         <ScrollView
           android:height="fill_parent"
           android:layout_above="@id/buttonParent" />
             [snip]
         </ScrollView>
    </RelativeLayout>
</LinearLayout>

0

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