将视图粘贴到scrollview的底部

时间:2018-04-10 00:35:19

标签: android android-layout android-scrollview

我有一个奇怪的行为,我无法弄清楚 - 我试图坚持视图到scrollview底部没有运气。我已经尝试过clipToPadding和fillViewport但它们都没有帮助。有帮助吗? 我的xml布局是 -

<LinearLayout>

    <FrameLayout/>
    <ScrollView>
        <LinearLayout>
            <LinearLayout/>
            <RelativeLayout/> <-- This is the problematic view
        </LinearLayout>
    </ScrollView>

</LinearLayout>

我想将相对布局粘贴到底部,即使滚动视图比屏幕长度短,当clipToPadding设置为false时它确实适合屏幕但是相对布局刚刚放置在屏幕中间的线性布局之后他,当我在scrollview上将fillviewport设置为true(并删除cliptopadding)时,scrollview比屏幕长但不可滚动导致relativelayout“隐形”,有什么建议?

2 个答案:

答案 0 :(得分:0)

尝试在滚动视图中使用相对布局而不是线性布局,并将相对布局与底部对齐。但我不确定它是否会在有内容后滚动。

<LinearLayout>

<FrameLayout/>
<ScrollView>
    <RelativeLayout>
        <LinearLayout android:alignParentTop="true" android:id="@+id/linearLayout" />
        <RelativeLayout android:alignParentBottom="true" android:layoutBelow="@+id/linearLayout"/> <-- This is the problematic view
    </LinearLayout>
</ScrollView>

检查这是否适合你。

答案 1 :(得分:0)

您可以尝试在ScrollView中使用ConstraintLayout:

  1. 将ScrollView中的fillviewport设置为True
  2. 最后一个元素必须在约束的底部附加上
  3. 最后一个元素(一个在最后一个元素之前)应该对最后一个元素设置约束。另外,您可以添加边距以使此元素与最后一个元素之间的距离最小,并使用constraintVertical_bias管理其位置。

<ScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fillViewport="true">

<androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

    … Some elements with Constraints …

    <TextView
            android:id="@+id/last_but_one_element”
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"                        
            app:layout_constraintBottom_toTopOf="@+id/some_previous_element"
            app:layout_constraintTop_toBottomOf="@+id/last_element"
            android:layout_marginBottom=“40dp"
            app:layout_constraintVertical_bias="0.1"/>

    <Button
            android:id="@+id/last_element”
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintBottom_toBottomOf="parent"/>

</androidx.constraintlayout.widget.ConstraintLayout>