【问题标题】:Sticking a view to bottom of scrollview将视图粘贴到滚动视图的底部
【发布时间】:2018-04-10 00:35:18
【问题描述】:

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

<LinearLayout>

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

</LinearLayout>

即使滚动视图比屏幕长度短,我也想将相对布局固定在底部,当 clipToPadding 设置为 false 时,它​​确实适合屏幕,但是相对布局在线性布局之后位于屏幕中间他,当我在滚动视图上将 fillviewport 设置为 true (并删除剪辑添加)时,滚动视图比屏幕长但不可滚动,导致相对布局“不可见”,有什么建议吗?

【问题讨论】:

    标签: android android-layout android-scrollview


    【解决方案1】:

    您可以尝试在 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>
    

    【讨论】:

    • 如果您不想在 textview 上方有任何间隙,则可以使用 app:layout_constraintVertical_bias="0"。
    【解决方案2】:

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

    <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>
    

    检查这是否适合您。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-07-10
      • 2011-08-31
      • 2011-01-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多