【问题标题】:How to stick a view at the bottom of the screen so the above part is movable?如何在屏幕底部粘贴视图以使上述部分可移动?
【发布时间】:2012-11-29 23:48:11
【问题描述】:

我的layout 包含一个包含所有内容的ScrollView。在某些时候,我需要在屏幕底部显示一个ViewScrollView 的顶部(上方)(所以ScrollView 位于View 的后面),这样我就可以向上滚动屏幕并向下,而这个View 仍然粘在设备的屏幕底部。请建议大家如何做这样的layout。谢谢。

【问题讨论】:

    标签: android android-layout


    【解决方案1】:

    如果您希望滚动视图位于视图后面,可以这样做:

    (来自this SO question的部分代码副本)

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical" android:layout_width="fill_parent"
        android:layout_height="fill_parent" android:id="@+id/RelativeLayout1">
    
            <ScrollView android:id="@+id/scrollView"  
                android:layout_width="fill_parent"
                android:layout_height="wrap_content">
            </ScrollView>
    
            <View android:id="@+id/bottomView" 
                android:layout_width="fill_parent"
                android:layout_height="wrap_content" 
                android:layout_alignParentBottom="true"
                android:visibility="gone">>
            </View>
    </RelativeLayout>
    

    滚动视图现在将扩展到视图元素之外。

    【讨论】:

    • 抱歉,我更正了一个问题,我需要将此 View 放在 ScrollView 之上。
    • 嗯,你是什么意思?在屏幕底部,在滚动视图的顶部?所以滚动视图落后于视图?您可以使用RelativeLayout 而不是LinearLayout 来做到这一点。如果您的意思是您希望 View 在屏幕顶部,在 ScrollView 的上方(顶部),只需将视图放在布局中的滚动视图上方即可。
    • 如果您的意思是第一个,那么这个 SO 问题:stackoverflow.com/questions/7442911/… 将您请求的功能描述为一个问题。
    • 我的意思是我需要在屏幕底部有一个 View,所以 scrollview 确实在 View 后面。
    • 酷!非常感谢,请删除第一部分,第二部分是我真正需要的。
    【解决方案2】:

    您可以创建两个片段并使用权重属性来分配空间。顶部的片段将承载您的根 scrollView,而底部的片段将拥有视图。

    【讨论】:

    • 我猜这里不需要碎片?
    【解决方案3】:

    好的,两个选项,第一个:

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >
    
    <View
        android:id="@+id/myView"
        android:layout_width="fill_parent"
        android:layout_height="1dp"
        android:layout_alignParentBottom="true" />
    
    <ScrollView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_above="@id/myView" />
    
    </RelativeLayout>
    

    这样您的ScrollView 将始终高于您的View。但似乎您希望 ScrollView 填满整个屏幕,而 View “隐藏”您的 SV 的某些项目。所以检查这段代码:

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >
    
     <ScrollView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"/>
    
     <View
        android:id="@+id/myView"
        android:layout_width="fill_parent"
        android:layout_height="1dp"
        android:layout_alignParentBottom="true" />
    
    </RelativeLayout>
    

    【讨论】:

    • 最好将视图设置为 wrap_content 或类似的东西,并使其不可见。既然你想要它,那就是隐形的。
    • 通常情况下是这样。但如果是View wrap_content 将始终填满整个屏幕。请不要问我为什么,但这就是我所经历的......
    猜你喜欢
    • 2021-11-30
    • 1970-01-01
    • 2011-02-03
    • 2015-07-05
    • 2021-01-08
    • 1970-01-01
    • 2015-03-05
    • 2020-01-14
    相关资源
    最近更新 更多