【问题标题】:How to implement a scroll view on only part of my layout如何仅在我的部分布局上实现滚动视图
【发布时间】:2012-06-25 11:19:17
【问题描述】:

我想创建一个布局,在最顶部有 6 个 TextView,在最底部有 4 个按钮,然后让整个中心空间成为一个带有 22 个复选框(每行 1 个)的滚动视图。基本上我想要一个顶部的标题栏,底部的 2 个导航按钮,以及一个可以在中间滚动的列表。

我还希望复选框保持不可选中状态,我确定这是一个简单的函数调用。

视图没有带有文本的上部标题,也没有带有按钮的下部栏。有谁知道该怎么做?滚动视图是唯一出现的东西......

<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:weightSum="1.0" >
    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1" >
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:gravity="center_horizontal"
                android:text="SN : " />

            <TextView
                android:id="@+id/serialNumberView"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:gravity="center_horizontal" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:gravity="center_horizontal"
                android:text="Ver : " />

            <TextView
                android:id="@+id/versionView"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:gravity="center_horizontal" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:gravity="center_horizontal"
                android:text="Type : " />

            <TextView
                android:id="@+id/typeView"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:gravity="center_horizontal" />

    </LinearLayout>

    <ScrollView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_weight="1" >

            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" 
                android:orientation="vertical"
                android:layout_weight="1">


                    <CheckBox
                        android:id="@+id/floatCheck"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="Float" />

                    <CheckBox
                        android:id="@+id/tripCheck"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="Trip" />

                    <CheckBox
                        android:id="@+id/closeCheck"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="Close" />

                    <CheckBox
                        android:id="@+id/blockedCheck"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="Blocked" />

                    <CheckBox
                        android:id="@+id/hardTripCheck"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="Hard Trip" />

                    <CheckBox 
                        android:id="@+id/phaseAngleCheck"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="Phase angle wrong for closing" />

                    <CheckBox
                        android:id="@+id/diffVoltsCheck"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="Differential volts too low" />

                    <CheckBox
                        android:id="@+id/networkVoltsCheck"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="Netwrok volts too low to close" />

                    <CheckBox
                        android:id="@+id/usingDefaultsCheck"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="Using Defaults( Reprogram )" />

                    <CheckBox
                        android:id="@+id/relaxedCloseActiveCheck"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="Relaxed Close Active" />

                    <CheckBox
                        android:id="@+id/commBoardDetectedCheck"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="Comm Board Detected" />

                    <CheckBox 
                        android:id="@+id/antiPumpBlock"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="Anti-Pump Block" />

                    <CheckBox
                        android:id="@+id/motorCutoffCheck"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="Motor Cutoff Inhibit" />

                    <CheckBox
                        android:id="@+id/phaseRotationCheck"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="Phase Rotation Wrong" />

                    <CheckBox
                        android:id="@+id/usingDefaultDNPCheck"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text= "Using Default DNP Profile" />
            </LinearLayout>
    </ScrollView>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_weight="1" >

        <Button
            android:id="@+id/button3"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="Back" />

        <Button
            android:id="@+id/button3"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="Read" />
    </LinearLayout>
</LinearLayout>

【问题讨论】:

  • 您想要一个不可选中的复选框?但是您使用微调器...如果我是您,我会使用列表视图。它应该在自身内部滚动 afaik。
  • 对不起,我完全搞砸了,我发布了错误的代码,但我想对这两个 XML 文件做类似的事情,一个有微调器,一个有复选框
  • 然后编辑它。还是。为什么你想要不可选中的复选框?改用按钮?
  • 对不起,我还没有编辑它,因为它实际上是同样的问题,一个只有一行“TextView - Spinner”而不是“Checkboxes”。不勾选框的原因是用户按下底部的读取按钮,它从另一个设备读取数据并通过勾选某些框来显示该设备的当前状态

标签: android user-interface scrollview


【解决方案1】:

这是根据您的要求的伪布局设计:

<LinearLayout
   ----
   android:weightSum="1.0" >

--- Your top elements ---

<ScrollView >
<LinearLayout
   ----
   android:layout_weight="1.0" >

--- Your scrollable items ----

</LinearLayout>
</ScrollView>

--- Your bottom elements ---

</LinearLayout>

【讨论】:

  • layout_weight 对视图有什么作用?
  • 权重将整个空间分配给固定顶部和底部视图的可滚动项目。您的项目将在顶部和底部滚动,如果您不需要此行为,请移除重量。
  • 这就是我想要的,但我的按钮底部视图没有出现。它们在自己的 LinearLayout 中,我需要给它们添加权重吗?
  • 在滚动视图内的线性布局中添加 android:layout_weight="1.0" 并从底部线性布局中删除 android:weightSum="1.0"。
  • 抱歉,还是没有。我已将上面的 XML 文件更新为我现在拥有的文件。在 scrollView 线性布局内的 layout_weight 行上有一条警告,上面写着“Invalid layout param in ScrollView: layout_weight”
【解决方案2】:

尝试做这样的事情:

LinearLayout
    LinearLayout
        Header-Elements
    /LinearLayout

    ScrollView
        LinearLayout
            Main-Elements
        /LinearLayout
    /ScrollView

    LinearLayout
        Bottom-Elements
    /LinearLayout
/LinearLayout

如果您希望元素显示在彼此下方,请记住在 LinearLayout 上使用 android:orientation="vertical"。

【讨论】:

  • 我认为这是我需要做的,但它仍然无法正常工作......我会更新我的帖子
  • 所有显示在我的图形布局中的是一个居中的 TextView,上面写着“SN:”,除此之外,它是空的
  • 在你的LinearLayout中设置android:orientation="vertical",否则第一个TextView会填满宽度,下一个TextView会在它旁边(屏幕外)
  • 按钮的底部视图没有出现,知道如何解决这个问题吗?
  • 设置上下LinearLayout的高度为“wrap_content”,ScrollView的高度为“fill_parent”。如果这还不起作用,请尝试在顶部和底部 LinearLayout 以及 ScrollView 上设置 android:weight="1"。
【解决方案3】:

您可以为此使用RelativeLayout:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@color/White" >

    <RelativeLayout 
        android:id="@+id/topLayout"
        android:layout_width="fill_parent"
        android:layout_height="match_parent"
        android:layout_alignParentTop="true" >
        // place your all the textViews here (all the top ones.)
    </RelativeLayout>

    <ScrollView
        android:layout_below="@id/topLayout"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >
        // place your all scrollable views here.
    </ScrollView>

    <RelativeLayout 
        android:id="@+id/bottomLayout"
        android:layout_width="fill_parent"
        android:layout_height="match_parent"
        android:layout_alignParentBottom="true" >
        // place your all the buttons here (the bottom part.)
    </RelativeLayout>
</RelativeLayout>

编辑:

由于RelativeLayout 放置视图相对,通过使用android:layout_alignParentBottom="true",视图可以放置在屏幕底部。

【讨论】:

  • 我不知道为什么我现在回过头来对此投了反对票,对不起:/ 如果您继续编辑它,我将删除反对票。现在回到这个问题,我知道得更清楚了,显而易见的答案是使用 RelativeLayout。
  • 我已经更新了答案。请删除反对票。如果您需要任何帮助来了解它的工作原理,请随时提出。现在您也同意使用 RelativeLayout 是一种更简单且正确的方法,您也可以接受答案。
【解决方案4】:

如果布局视图的某些元素没有显示,您可以随时使用该元素来包装现有内容。

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

【讨论】:

    【解决方案5】:

    在您的情况下,您有一个线性根视图,其中包含其他三个独立的子视图。将scrollView的高度设置为fill_parent,使其占用父视图的大小,从而阻止其他具有wrap_content高度的子视图显示。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多