Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/202.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android/XML-如何将固定布局与父级顶部对齐,并在下面显示scrollview?_Android_Xml_Android Layout_Android Scrollview - Fatal编程技术网

Android/XML-如何将固定布局与父级顶部对齐,并在下面显示scrollview?

Android/XML-如何将固定布局与父级顶部对齐,并在下面显示scrollview?,android,xml,android-layout,android-scrollview,Android,Xml,Android Layout,Android Scrollview,请参考下面的例子。我想让顶部布局(下面用红色包围)在活动的滚动视图中不移动。我有一个scrollview作为父布局,然后我认为在顶部有一个相对布局是可行的,并将其与顶部对齐,但这并没有真正起作用,因为它仍然保留在scrollview中。我想让用户有红色的布局框保持静态,当他们向下滚动 我想我还必须在scrollview的顶部添加一个topMargin或其他内容,以适应redbox布局 此处发布的XML代码:您应该将ScrollView仅用于一个子项(官方文档-)。根据xml,ScrollVie

请参考下面的例子。我想让顶部布局(下面用红色包围)在活动的滚动视图中不移动。我有一个scrollview作为父布局,然后我认为在顶部有一个相对布局是可行的,并将其与顶部对齐,但这并没有真正起作用,因为它仍然保留在scrollview中。我想让用户有红色的布局框保持静态,当他们向下滚动

我想我还必须在scrollview的顶部添加一个topMargin或其他内容,以适应redbox布局


此处发布的XML代码

您应该将ScrollView仅用于一个子项(官方文档-)。根据xml,ScrollView非常复杂,有很多子部件

最好的选择是使用LinearLayout作为整个容器的根,使用LinearLayout(或相对)作为包含重置和保存按钮的顶部布局,使用ListView作为您拥有的长列表。ListView负责自己的滚动。所以你不必为此担心

这也将提高代码性能。

执行类似操作(手动代码,仅供参考):


//或者你想要的任何其他高度
//俯视图的内容
//滚动视图的内容

请注意,不要像这样将子项硬编码到
滚动视图中。使用
RecyclerView
(它是
ListView
的更新现代替代品),如果您想进入严肃的Android编程领域,您应该知道如何使用它。它实际上非常容易使用,一旦你掌握了窍门:-)

这应该适合你的需要:

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <RelativeLayout android:id="@+id/topPanel"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="5dp">

        <TextView android:id="@+id/label"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:text="Multi TTS Implementation"/>


        <Button android:id="@+id/save"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"
            android:text="SAVE"/>

        <Button android:id="@+id/resetAll"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_toLeftOf="@id/save"
            android:layout_centerVertical="true"
            android:text="RESET ALL"/>

    </RelativeLayout>

    <ScrollView android:id="@id/scroll"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_below="@id/topPanel"
        android:layout_alignParentBottom="true"
        android:padding="5dp">

        <!-- Your scrollable content here -->

    </ScrollView>

</RelativeLayout>


不要将ScrollView设置为根目录,而是垂直线性布局{Immobile,ScrollView}。另外,我建议您使用ListView,而不是带有ScrollView硬编码项的疯狂xml。ScrollView只有一个子目录。这是一个单一的布局。但是,该布局下有更多的布局和子级,它们以按钮和编辑文本结尾。那么我将查看listview。
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <RelativeLayout android:id="@+id/topPanel"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="5dp">

        <TextView android:id="@+id/label"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:text="Multi TTS Implementation"/>


        <Button android:id="@+id/save"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"
            android:text="SAVE"/>

        <Button android:id="@+id/resetAll"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_toLeftOf="@id/save"
            android:layout_centerVertical="true"
            android:text="RESET ALL"/>

    </RelativeLayout>

    <ScrollView android:id="@id/scroll"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_below="@id/topPanel"
        android:layout_alignParentBottom="true"
        android:padding="5dp">

        <!-- Your scrollable content here -->

    </ScrollView>

</RelativeLayout>