java - 有人可以解释为什么我不能向这个 ScrollView 添加额外的按钮吗?

标签 java android button scrollview

这毫无意义。我有一些代码有一个简单的 LinearLayout,里面有一个 TextView 和一个 ScrollView,在 ScrollView 里面有一个按钮。好吧,当我添加第二个按钮时,我收到一条错误消息,指出 ScrollView 只能有一个 child 。 Button 是否被视为不同的 child ???如果这真的很简单,我为我的愚蠢感到抱歉。如果有人可以提供帮助,谢谢!

<?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>

最佳答案

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

A ScrollView is a FrameLayout, meaning you should place one child in it containing the entire contents to scroll; this child may itself be a layout manager with a complex hierarchy of objects. A child that is often used is a LinearLayout in a vertical orientation, presenting a vertical array of top-level items that the user can scroll through.

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

关于java - 有人可以解释为什么我不能向这个 ScrollView 添加额外的按钮吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8373730/

相关文章:

java - 使用相同的变量打印收据,因此每个项目都会传递相同的值。

java.lang.IndexOutOfBoundsException : Invalid index 0, 大小为 0

java - 增加 ActionListener 内的值的问题

java - querydsl - 列表条件

java - Android Socket不发送数据

安卓异常 : Error inflating class fragment

java - StateBased Java Game - 如何判断鼠标按钮是否被释放?

android - 如何知道哪个按钮调用了方法?

Android:将按钮添加到首选项屏幕

java - 从Java中的文件读取错误