服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - Android - Android ScrollView只能添加一个子控件问题解决方法

Android ScrollView只能添加一个子控件问题解决方法

2021-06-17 15:14炫_愛羊 Android

这篇文章主要介绍了Android ScrollView只能添加一个子控件问题解决方法,涉及Android界面布局的相关技巧,需要的朋友可以参考下

本文实例讲述了Android ScrollView只能添加一个子控件问题解决方法。分享给大家供大家参考,具体如下:

有下面一段代码

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<?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" >
  <ScrollView
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >
    <Button
      android:layout_width="fill_parent"
      android:layout_height="wrap_content" />
    <Button
      android:layout_width="fill_parent"
      android:layout_height="wrap_content" />
    <Button
      android:layout_width="fill_parent"
      android:layout_height="wrap_content" />
  </ScrollView>
</LinearLayout>

一个ScrollView里面添加了三个Button,也许你认为没有什么问题,那么我们运行一下看看

出现了一个异常

Android ScrollView只能添加一个子控件问题解决方法

很明显,异常告诉我们ScrollView can host only one direct child

既然说只能容纳一个直接的子控件,那么我们就可以容纳多个间接的子控件,直接在这些子控件外面再套一层LinearLayout就OK了

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<?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" >
  <ScrollView
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >
    <LinearLayout
      android:layout_width="fill_parent"
      android:layout_height="fill_parent"
      android:orientation="vertical" >
      <Button
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />
      <Button
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />
      <Button
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />
    </LinearLayout>
  </ScrollView>
</LinearLayout>

希望本文所述对大家Android程序设计有所帮助。

延伸 · 阅读

精彩推荐