【问题标题】:Linear layout above a scrollview滚动视图上方的线性布局
【发布时间】:2011-12-16 19:35:25
【问题描述】:

我有一个简单的 android 布局,其中包含一个包含 tablelayout 的滚动视图。该表正确显示,但我想在滚动视图上方添加一个静态标题,以便基本上表中列的标签始终保持在屏幕顶部。但是,当我在布局文件中的<ScrollView> 上方添加<LinearLayout> 时,它会全部消失。

谁能发现我的 xml 文件做错了什么?

<?xml version="1.0" encoding="utf-8"?>

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/linearLayout1"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:gravity="top" >

        <LinearLayout 
            android:id="@+id/header"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" 
            />

        <ScrollView
            android:id="@+id/scrollView1"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent" >

            <TableLayout 
                android:id="@+id/mainTable"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent" />
        </ScrollView>

    </LinearLayout>

事实上,我看不到滚动视图或表格布局,但如果我删除 header 线性布局,它会显示得很好。

【问题讨论】:

  • 你为什么不设置任何LinearLayouts 的方向?
  • 因为这正是我需要做的......让它成为一个答案,这样我就可以接受它

标签: android scrollview android-linearlayout


【解决方案1】:

LinearLayouts需要设置android:orientation参数。

【讨论】:

  • 是的,出于某种原因,这是问题所在,我将主布局设置为 vertical 方向,内部布局设置为 horizontal 方向。现在我只需要弄清楚为什么内部布局占用了比需要更多的垂直空间......
  • 可能是因为main和scroll都设置为fill_parent..!?
  • 好吧,它的滚动视图(设置为fill_parent)没有占用足够的空间。出于某种原因,当我在内部 LinearLayout 的某些子视图中设置文本时,它会导致它扩展太多
【解决方案2】:

您必须将父 LinearLayout 的方向设置为垂直!默认设置为水平,这样您就看不到 ScrollView。

你可以试试这个:

<LinearLayout 
        android:id="@+id/header"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" 
        android:layout_weight="0"
        />

    <ScrollView
        android:id="@+id/scrollView1"
        android:layout_width="fill_parent"
        android:layout_height="0dp" android:layout_weight="1">

        <TableLayout 
            android:id="@+id/mainTable"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent" />
    </ScrollView>

希望这会有所帮助!

【讨论】:

    【解决方案3】:

    您的滚动视图的 layout_height 设置为 fill_parent。使用 wrap_content。

    【讨论】:

    • 还使用 wrap_content 作为 TableLayout 的高度。我想这是在我的回答中给出的,但显然不是。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-09
    • 1970-01-01
    • 2023-04-02
    • 1970-01-01
    相关资源
    最近更新 更多