【问题标题】:Can someone explain why I can't add an extra button to this ScrollView?有人可以解释为什么我不能向这个 ScrollView 添加一个额外的按钮吗?
【发布时间】:2011-12-04 06:26:17
【问题描述】:

这毫无意义。我有一些代码,它有一个简单的 LinearLayout,里面有一个 TextView 和一个 ScrollView,在 ScrollView 里面有一个按钮。好吧,当我添加第二个按钮时,我收到一条错误消息,说 ScrollView 只能有一个孩子。一个按钮被认为是一个不同的孩子???如果这真的很简单,我很抱歉我的愚蠢。如果有人能帮忙谢谢!

<?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:duplicateParentState="false"
android:fadeScrollbars="true"
android:gravity="center"
android:isScrollContainer="true"
android:orientation="vertical"
android:scrollbars="vertical">
<TextView 
    android:text="Select category:"
    android:textStyle="italic"
    android:textColor="#000000"
    android:textSize="20dp"
    android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

<ScrollView
    android:id="@+id/ScrollView01"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:fadingEdgeLength="100dp">

        <Button
            android:id="@+id/Food"
            android:layout_width="175dp"
            android:layout_height="65dp"
            android:layout_gravity="center"
            android:gravity="center"
            android:background="@drawable/blackbutton"
            android:text="Food"
            android:textColor="#ffffff" />
        <Button
            android:id="@+id/Clothing"
            android:layout_width="175dp"
            android:layout_height="65dp"
            android:layout_gravity="center"
            android:gravity="center"
            android:background="@drawable/blackbutton"
            android:text="Clothing"
            android:textColor="#ffffff" />  
</ScrollView>
</LinearLayout>

【问题讨论】:

    标签: java android button scrollview


    【解决方案1】:

    您的答案确实在ScrollView 的文档中:

    ScrollView 是 FrameLayout,这意味着您应该在其中放置一个孩子 它包含要滚动的全部内容;这个孩子本身可能是 具有复杂对象层次结构的布局管理器。一个孩子是 经常使用的是一个垂直方向的LinearLayout,呈现一个 用户可以滚动浏览的顶级项目的垂直数组。

    换句话说:您需要将两个按钮放在您选择的布局中,而这又是 ScrollView 的唯一子级。

    【讨论】:

      【解决方案2】:

      ScrollView 必须有 1 个孩子。所以把这两个按钮放在一个布局中。

      【讨论】:

        【解决方案3】:

        将这两个按钮放在线性或相对布局中......并将该布局放在滚动视图中。这应该可以解决问题。

        滚动视图只能有一个子控件...

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2022-01-22
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-02-21
          • 2010-12-17
          • 2014-12-06
          相关资源
          最近更新 更多