【问题标题】:scrollview with floating linearlayout will not scroll带有浮动线性布局的滚动视图不会滚动
【发布时间】:2011-04-08 09:43:00
【问题描述】:

这是我的 gui xml:

<FrameLayout android:layout_width="fill_parent"
    android:layout_height="fill_parent" android:id="@+id/frameLayout"
    xmlns:android="http://schemas.android.com/apk/res/android">
    <ScrollView android:layout_width="fill_parent" android:id="@+id/scrollView"
        android:fillViewport="true" android:layout_height="fill_parent">

        <LinearLayout android:layout_width="fill_parent"
            android:orientation="vertical" android:id="@+id/contentLayout"
            android:padding="5dip" android:fadingEdge="vertical"
            android:layout_height="fill_parent">
            <TextView android:id="@+id/lblName" android:layout_width="fill_parent"
                android:layout_height="wrap_content" android:text="@string/label_name"></TextView>
            <LinearLayout android:layout_width="fill_parent"
                android:layout_height="wrap_content" android:id="@+id/nameLayout">
                <CheckBox android:layout_width="wrap_content"
                    android:layout_height="wrap_content" android:id="@+id/chkName"></CheckBox>
                <TextView android:layout_marginLeft="4dip"
                    android:layout_marginRight="4dip" android:id="@+id/txtName"
                    android:layout_width="wrap_content" android:layout_height="wrap_content"
                    android:textColor="@color/white" android:text="TextView"
                    android:layout_weight="1" android:textSize="20dip"></TextView>
            </LinearLayout>
            <TextView android:id="@+id/lblPhone" android:layout_width="fill_parent"
                android:layout_height="wrap_content" android:text="@string/label_phone"></TextView>
            <LinearLayout android:isScrollContainer="false"
                android:layout_width="fill_parent" android:layout_height="wrap_content"
                android:orientation="vertical" android:id="@+id/lstPhone"></LinearLayout>
            <TextView android:id="@+id/lblEmail" android:layout_width="fill_parent"
                android:layout_height="wrap_content" android:text="@string/label_email"></TextView>
            <LinearLayout android:layout_width="fill_parent"
                android:layout_height="wrap_content" android:orientation="vertical"
                android:id="@+id/lstEmail"></LinearLayout>
            <TextView android:id="@+id/lblAddress" android:layout_width="fill_parent"
                android:layout_height="wrap_content" android:text="@string/label_address"></TextView>
            <LinearLayout android:layout_width="fill_parent"
                android:layout_height="wrap_content" android:orientation="vertical"
                android:id="@+id/lstAddress"></LinearLayout>
        </LinearLayout>
    </ScrollView>
    <LinearLayout android:background="@color/grey"
        android:layout_width="fill_parent" android:paddingTop="5dip"
        android:layout_height="wrap_content" android:orientation="horizontal"
        android:layout_gravity="bottom|right" android:id="@+id/commandLayout">
        <Button android:id="@+id/btnToClipBoard" android:layout_width="fill_parent"
            android:layout_height="fill_parent" android:text="To ClipBoard"
            android:layout_weight="1"></Button>
        <Button android:id="@+id/btnCancel" android:layout_width="fill_parent"
            android:layout_height="fill_parent" android:text="Cancel"
            android:layout_weight="1"></Button>
    </LinearLayout>
</FrameLayout>

LinearLayout id: commandLayout 很好地停留在窗口底部。但是 LinearLayout id: contentLayout 不会滚动。如果我将 commandLayout 移动到 scrollView 内,一切都会滚动。但这不是我想要的。无论 contentLayout 有多长,我都想看到按钮。任何帮助将不胜感激。我花了大部分时间在这上面...谢谢。

编辑:感谢所有回复。从 FrameLayout 更改为 RelativeLayout 并设置 commandLayout align parent bottom 和 scrollview align parent top 是效果最好的解决方案。

【问题讨论】:

  • 如果它有助于我在创建活动时使用 layoutInflater 添加项目...
  • 只是猜测:将ScrollView 的高度设置为wrap_content 并添加weight=1。为什么在宽度为fill_parentcommandLayout 中需要gravity bottom|right? mb bottom 就够了吗?
  • 感谢您的回复,您的建议我已经试过了,还是不行。
  • 我使用的是HorizontalScrollView 而不是ScrollView ​​ntalScrollView>
  • 放置更多项目并删除所有不必要的属性。放置很多元素,以便它可以滚动。只是一个想法。

标签: android user-interface android-layout


【解决方案1】:

我将基本布局(即“frameLayout”)更改为相对布局。

我这样做是为了让“commandLayout”位于“scrollView”下方。 如果您要显示的内容隐藏在“commandLayout”后面,那是没有意义的

我在“commandLayout”上添加了 alignParentBottom。 我还在“scrollView”中添加了 layout_above="@+id/commandLayout"

这是工作 xml,是的,它确实滚动 =)

<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/relativeLayout"
xmlns:android="http://schemas.android.com/apk/res/android"
>
<ScrollView 
    android:layout_width="fill_parent"
    android:id="@+id/scrollView"
    android:fillViewport="true"
    android:layout_height="fill_parent"
    android:layout_above="@+id/commandLayout"
>
    <LinearLayout 
        android:layout_width="fill_parent"
        android:orientation="vertical"
        android:id="@+id/contentLayout"
        android:padding="5dip"
        android:fadingEdge="vertical"
        android:layout_height="fill_parent"
    >
        <TextView
            android:id="@+id/lblName"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
        >
        </TextView>
        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:id="@+id/nameLayout"
        >
            <CheckBox 
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/chkName">
            </CheckBox>
            <TextView
                android:layout_marginLeft="4dip"
                android:layout_marginRight="4dip"
                android:id="@+id/txtName"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textColor="#FFFFFF"
                android:text="TextView"
                android:layout_weight="1"
                android:textSize="20dip">
            </TextView>
        </LinearLayout>

        <TextView
            android:id="@+id/lblPhone"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
        >
        </TextView>
        <LinearLayout
            android:isScrollContainer="false"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:id="@+id/lstPhone">
        </LinearLayout>
        <TextView
            android:id="@+id/lblEmail"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
        >
        </TextView>
        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:id="@+id/lstEmail"
        >
        </LinearLayout>
        <TextView
            android:id="@+id/lblAddress"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
        >
        </TextView>
        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:id="@+id/lstAddress">
        </LinearLayout>
        <TextView
            android:id="@+id/LongText"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:textSize="40sp"
            android:text="This is a super long text that will hopefully cover the whole screen so as the scroll will work."
        >
        </TextView>

         <TextView
            android:id="@+id/AnotherLongText"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:textSize="40sp"
            android:text="This is another super long text to show that the scrolling will work even more!"
        >
        </TextView>
    </LinearLayout>
</ScrollView>

<LinearLayout
    android:background="#737754"
    android:layout_width="fill_parent"
    android:paddingTop="5dip"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:id="@+id/commandLayout"
    android:layout_alignParentBottom="true"
>
    <Button
        android:id="@+id/btnToClipBoard"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:text="To ClipBoard"
        android:layout_weight="1">
    </Button>
    <Button
        android:id="@+id/btnCancel"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:text="Cancel"
        android:layout_weight="1">
    </Button>
</LinearLayout>
</RelativeLayout>

【讨论】:

  • 不要忘记你的结束标签...谢谢
  • =) 它没有被代码框捕获。感谢您指出。
【解决方案2】:

有必要使用FrameLayout吗?如果我正确理解了您的目标,RelativeLayout 可能达到了您的预期;

<RelativeLayout ...>
    <LinearLayout
        android:layout_alignParentBottom="true"
        android:id="@+id/commandLayout" ... />
    <ScrollView
        android:layout_alignParentTop="true"
        android:layout_above="@id/commandLayout"
        android:id="@+id/scrollView" .../>
</RelativeLayout>

【讨论】:

  • 现在,在我注意到您希望“commandLayout”浮动在 ScrollView 上之后,如果您将两个元素与父边界对齐,也可以使用 RelativeLayout。但这是我目前无法验证的事情——尤其是不知道它是否比使用 FrameLayout 更好。
【解决方案3】:

我可能没有完全理解问题是什么,但是这里......我尝试了您放置的布局文件,发现列表底部隐藏在两个按钮后面。所以我在 ScrollView 小部件中添加了以下内容,看起来还不错。

android:layout_marginBottom="60dip"

但是,也许我错过了什么。

【讨论】:

  • 是的,它会滚动。我在列表中添加了一些复选框,使其比页面长。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-03-09
  • 1970-01-01
相关资源
最近更新 更多