如何在 Android xml 中的线性布局内设置滚动视图?

How to set a scroll view inside a Linear Layout in Android xml?

我有这个 xml 代码,我想为这些 buttons.Can 设置滚动视图有人帮我吗?当我在 layout.The xml 中设置包含一些按钮和文本视图的滚动视图时显示错误。文本视图和按钮在垂直方向的线性布局内水平对齐。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.cozyne.toglebtn.MainActivity" >


<ScrollView
    android:id="@+id/scrollView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true" > 



<LinearLayout
    android:id="@+id/llTopBar"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:divider="#00ff00"
    android:dividerPadding="22dip"
    android:orientation="horizontal"
    android:showDividers="middle" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="75dp"
        android:layout_height="wrap_content"
        android:text="12:00 AM" />
 <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="62dp"
        android:text="ON" />
</LinearLayout>
<LinearLayout
    android:id="@+id/layout2"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:divider="#00ff00"
    android:dividerPadding="22dip"
    android:orientation="horizontal"
    android:showDividers="middle" >
<TextView
        android:id="@+id/textView3"
        android:layout_width="75dp"
        android:layout_height="wrap_content"
        android:text="12:00 AM" 
<Button
        android:id="@+id/button3"
        android:layout_width="wrap_content"
        android:layout_height="62dp"
        android:text="ON" />
 </LinearLayout>
</ScrollView>

问题是您还没有结束 xml。你必须把

</LinearLayout> 

标签在最后。您开始使用 LinearLayout 作为父容器并放入 scrollView 中。然后你再放一个 LinearLayout 里面等等......。但是在你的 xml 结束时,你没有关闭父 LinearLayout....

第二个问题是,您在 ScrollView 中有多个视图。里面有两个 LinearLayouts,只需在 scrollView 中创建一个视图。不允许在 scrollView 中放置多个...

编辑

最后,如 Rubén Jiménez 所说,您错过了 textView 末尾的标签。该编辑只是为了完整性...

ScrollView 只能有一个直接子视图

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
                xmlns:tools="http://schemas.android.com/tools"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical"
                tools:context="com.cozyne.toglebtn.MainActivity" >

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true" 
        android:orientation="vertical"> 

    <LinearLayout
        android:id="@+id/llTopBar"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:divider="#00ff00"
        android:dividerPadding="22dip"
        android:orientation="horizontal"
        android:showDividers="middle" >

        <TextView
            android:id="@+id/textView1"
            android:layout_width="75dp"
            android:layout_height="wrap_content"
            android:text="12:00 AM" />
     <Button
            android:id="@+id/button2"
            android:layout_width="wrap_content"
            android:layout_height="62dp"
            android:text="ON" />
    </LinearLayout>
    <LinearLayout
        android:id="@+id/layout2"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:divider="#00ff00"
        android:dividerPadding="22dip"
        android:orientation="horizontal"
        android:showDividers="middle" >
    <TextView
            android:id="@+id/textView3"
            android:layout_width="75dp"
            android:layout_height="wrap_content"
            android:text="12:00 AM" />
    <Button
            android:id="@+id/button3"
            android:layout_width="wrap_content"
            android:layout_height="62dp"
            android:text="ON" />
     </LinearLayout>
     </LinearLayout>
   </ScrollView>

您还错过了 textView 末尾的“/>”。

更改为:

<TextView
        android:id="@+id/textView3"
        android:layout_width="75dp"
        android:layout_height="wrap_content"
        android:text="12:00 AM" />

试试这个最简单的解决方案:

 <?xml version="1.0" encoding="utf-8"?>
   <LinearLayout 
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   xmlns:android="http://schemas.android.com/apk/res/android">
   <ScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <LinearLayout 
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:orientation="vertical">
              <!-- Add up your Content -->
        </LinearLayout>
  </ScrollView>