【问题标题】:Adding Scrollview in Android Project Layout在 Android 项目布局中添加 Scrollview
【发布时间】:2016-08-15 20:02:17
【问题描述】:

我试图让我的布局向下滚动。我看过一些关于此的帖子,我尝试按照他们的解释进行操作,但这对我不起作用。这就是我现在拥有的,现在我的项目甚至无法启动,可能是因为它放错地方了。

这就是我的代码的样子:

<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.example.rodekruis.Bezoek" >

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="right"
        android:layout_marginBottom="20dp"
        android:src="@drawable/rkz_logo" />


     <ScrollView
    android:id="@+id/scrollview"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >



    <TextView
        android:id="@+id/textView1"
        android:layout_width="244dp"
        android:layout_height="276dp"
        android:layout_gravity="center"
        android:text="@string/title_activity_contact"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:textColor="@color/black" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="244dp"
        android:layout_height="42dp"
        android:layout_gravity="center"
        android:layout_weight="0.39"
        android:text="Klik hier voor de uitgebreide contactgegevens."
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:textColor="@color/black" />
   </ScrollView> 
</LinearLayout>

【问题讨论】:

  • Scrollview只有一个child,可以是一个view group

标签: java android eclipse layout scrollview


【解决方案1】:

ScrollView 只能有一个孩子,因此您应该添加一个 Layout Manager,将所有布局包装在 ScrollView 中。

<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.example.rodekruis.Bezoek" >
  ...
  <ScrollView
    android:id="@+id/scrollview"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >

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

    <TextView
        android:id="@+id/textView1"
        .../>

    <TextView
        android:id="@+id/textView2"
        .../>

    </LinearLayout>
  </ScrollView> 
   ...

【讨论】:

  • 整个代码给了我错误:XML 文档必须以相同的实体开始和结束
  • @SneakyAndStuff 你关闭了LinearLayout 标签。确保正确打开和关闭 xml 标签
  • 那个,能启动应用,还是不能向下滚动。
  • 您的 textview 是否足够长,可以滚动?
  • 是的,我专门为此添加了文字
【解决方案2】:

Scrollview 只有一个孩子,而你的layuot 有两个孩子,请使用这个 Spinets

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

<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="right"
    android:layout_marginBottom="20dp" />


<ScrollView
    android:id="@+id/scrollview"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <TextView
            android:id="@+id/textView1"
            android:layout_width="244dp"
            android:layout_height="276dp"
            android:layout_gravity="center"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:textColor="@color/black" />

        <TextView
            android:id="@+id/textView2"
            android:layout_width="244dp"
            android:layout_height="42dp"
            android:layout_gravity="center"
            android:layout_weight="0.39"
            android:text="Klik hier voor de uitgebreide contactgegevens."
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:textColor="@color/black" />
    </LinearLayout>


</ScrollView>

【讨论】:

  • 整个代码给了我错误:XML 文档必须以相同的实体开始和结束
  • 最后添加
  • 在显示中我们没有显示LinearLayout的结束标签,你应该在下面添加
  • 那个,能启动应用,还是不能向下滚动。
  • 它的工作,尝试在线性布局中添加更多的文本视图