中间需要 Header、Footer 和 ScrollView。当前代码不工作
Need a Header, Footer, and ScrollView in between. Current code not working
过去一个小时我一直在搜索这里和其他网站,试图找出我做错了什么。正如标题所示,我需要一个带按钮的 header、一个滚动视图,然后是一个带按钮的页脚。我希望 header 和页脚始终可见。找到的所有内容都说要制作相对布局,将其对齐到 parent 顶部,创建一个页脚,将其对齐到 parent 底部,然后创建一个滚动视图,将其设置在页脚上方和 [=33= 下方].出于某种原因,我的代码只是决定忽略它。
<RelativeLayout
android:id="@+id/topBar_id"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:background="@color/colorBackground">
<Button
android:id="@+id/charInfoBtn_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="General"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"/>
</RelativeLayout>
<ScrollView
android:id="@+id/genLayoutScroll_id"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="@id/bottomBar_id"
android:layout_below="@id/topBar_id>
<LinearLayout
//bunch of junk that users scroll through
</LinearLayout>
</ScrollView>
<RelativeLayout
android:id="@+id/bottomBar_id"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
android:background="@color/colorBackground">
<Button
android:id="@+id/genApplyBtn_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Apply"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent" />
</RelativeLayout>
将其用作我当前的代码,滚动视图位于顶部栏和底部栏的顶部。
如果我删除 layout_above 和 layout_below 行,并将其替换为
app:layout_constraintBottom_toTopOf="@id/bottomBar_id" 然后滚动视图不再越过我的底部栏,但现在是顶部栏,并且缺少滚动视图的顶部。
然后我将 app:layout_constraintTop_toBottomOf="@id/topBar_id" 添加到滚动视图,它又回到与具有 layout_below 和 layout_above 相同的状态。
我确定我在这里遗漏了一些东西,我只是无法弄清楚我遗漏了什么。希望有人能指出我犯的愚蠢错误。
P.S。我知道我的一些代码现在有点草率,主要是因为试图找出这个问题。
到目前为止,最简单的方法是使用垂直 LinearLayout
作为视图的根,并使用 "layout weight" 的概念制作中间元素( ScrollView
) 填满屏幕。
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:id="@+id/charInfoBtn_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="General"/>
<ScrollView
android:id="@+id/genLayoutScroll_id"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
<!-- your contents here -->
</ScrollView>
<Button
android:id="@+id/genApplyBtn_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Apply"/>
</LinearLayout>
如果您需要在每个栏中放置多个按钮,您可以将它们包裹在一个水平的 LinearLayout
中。
这样做的原因是 <ScrollView>
标签上的 android:layout_weight="1"
属性。为单个视图分配权重将使其占用所有 remaining space,在其他视图获取它们需要的内容之后。所以你的按钮得到了他们想要的 space ,而滚动视图得到了剩下的。由于它在中间,这必然会将底部按钮向下推到屏幕的末尾。
过去一个小时我一直在搜索这里和其他网站,试图找出我做错了什么。正如标题所示,我需要一个带按钮的 header、一个滚动视图,然后是一个带按钮的页脚。我希望 header 和页脚始终可见。找到的所有内容都说要制作相对布局,将其对齐到 parent 顶部,创建一个页脚,将其对齐到 parent 底部,然后创建一个滚动视图,将其设置在页脚上方和 [=33= 下方].出于某种原因,我的代码只是决定忽略它。
<RelativeLayout
android:id="@+id/topBar_id"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:background="@color/colorBackground">
<Button
android:id="@+id/charInfoBtn_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="General"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"/>
</RelativeLayout>
<ScrollView
android:id="@+id/genLayoutScroll_id"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="@id/bottomBar_id"
android:layout_below="@id/topBar_id>
<LinearLayout
//bunch of junk that users scroll through
</LinearLayout>
</ScrollView>
<RelativeLayout
android:id="@+id/bottomBar_id"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
android:background="@color/colorBackground">
<Button
android:id="@+id/genApplyBtn_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Apply"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent" />
</RelativeLayout>
将其用作我当前的代码,滚动视图位于顶部栏和底部栏的顶部。
如果我删除 layout_above 和 layout_below 行,并将其替换为 app:layout_constraintBottom_toTopOf="@id/bottomBar_id" 然后滚动视图不再越过我的底部栏,但现在是顶部栏,并且缺少滚动视图的顶部。
然后我将 app:layout_constraintTop_toBottomOf="@id/topBar_id" 添加到滚动视图,它又回到与具有 layout_below 和 layout_above 相同的状态。
我确定我在这里遗漏了一些东西,我只是无法弄清楚我遗漏了什么。希望有人能指出我犯的愚蠢错误。
P.S。我知道我的一些代码现在有点草率,主要是因为试图找出这个问题。
到目前为止,最简单的方法是使用垂直 LinearLayout
作为视图的根,并使用 "layout weight" 的概念制作中间元素( ScrollView
) 填满屏幕。
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:id="@+id/charInfoBtn_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="General"/>
<ScrollView
android:id="@+id/genLayoutScroll_id"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
<!-- your contents here -->
</ScrollView>
<Button
android:id="@+id/genApplyBtn_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Apply"/>
</LinearLayout>
如果您需要在每个栏中放置多个按钮,您可以将它们包裹在一个水平的 LinearLayout
中。
这样做的原因是 <ScrollView>
标签上的 android:layout_weight="1"
属性。为单个视图分配权重将使其占用所有 remaining space,在其他视图获取它们需要的内容之后。所以你的按钮得到了他们想要的 space ,而滚动视图得到了剩下的。由于它在中间,这必然会将底部按钮向下推到屏幕的末尾。