首页 > 解决方案 > 滚动视图中的线性布局

问题描述

在手机上测试时,滚动视图中的线性布局不起作用

我已经将线性布局放在相对布局的滚动视图中,当我在手机上测试它时它没有向下滚动

我已将图像视图和标题注册放在滚动视图之外,文本框和提交按钮放在我放置在滚动视图内的线性布局内。

请帮助我尝试了这么久的代码。我在滚动视图中尝试了相对布局并且设计显示正确但是在手机上测试时它没有在手机中向下滚动(在这两种情况下)

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
 xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:app="http://schemas.android.com/apk/res-auto"
 xmlns:tools="http://schemas.android.com/tools"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:background="@color/MistyRose"
 android:orientation="vertical"
 android:paddingLeft="16dp"
 android:paddingTop="16dp"
 android:paddingRight="16dp"
 android:paddingBottom="16dp">

 <ImageView
    android:id="@+id/imageView2"
    android:layout_width="203dp"
    android:layout_height="95dp"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="50dp"
    android:background="@drawable/logo" />

 <TextView
    android:id="@+id/textView"
    android:layout_width="wrap_content"
    android:layout_height="70dp"
    android:layout_alignBottom="@id/imageView2"
    android:layout_centerHorizontal="true"
    android:layout_marginBottom="-120dp"
    android:text="Register"
    android:textColor="@color/Black"
    android:textSize="40dp"
    android:textStyle="italic" />

 <ScrollView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@id/textView"
    android:layout_marginBottom="-520dp">

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

        <EditText
            android:id="@+id/Namebox"
            android:layout_width="300dp"
            android:layout_height="50dp"
            android:layout_marginStart="30dp"
            android:background="@drawable/edittext"
            android:ems="25"
            android:hint="Name"
            android:inputType="text" />

        <EditText
            android:id="@+id/Enailbox"
            android:layout_width="300dp"
            android:layout_height="50dp"
            android:layout_marginStart="30dp"
            android:layout_marginTop="30dp"
            android:background="@drawable/edittext"
            android:hint="Email"
            android:inputType="textEmailAddress" />

        <EditText
            android:id="@+id/Phonebox"
            android:layout_width="300dp"
            android:layout_height="50dp"
            android:layout_marginStart="30dp"
            android:layout_marginTop="30dp"
            android:background="@drawable/edittext"
            android:hint="Phone"
            android:inputType="phone" />

        <EditText
            android:id="@+id/Passbox1"
            android:layout_width="300dp"
            android:layout_height="50dp"
            android:layout_marginStart="30dp"
            android:layout_marginTop="30dp"
            android:background="@drawable/edittext"
            android:hint="Password"
            android:inputType="textPassword" />

        <EditText
            android:id="@+id/Passbox2"
            android:layout_width="300dp"
            android:layout_height="50dp"
            android:layout_marginStart="30dp"
            android:layout_marginTop="30dp"
            android:background="@drawable/edittext"
            android:hint="Password Confirm"
            android:inputType="textPassword" />

        <Button
            android:layout_width="150dp"
            android:layout_height="50dp"
            android:layout_marginStart="100dp"
            android:layout_marginTop="50dp"
            android:background="@drawable/button"
            android:text="Submit" />

    </LinearLayout>


  </ScrollView>

  </RelativeLayout>

标签: androidandroid-layoutandroid-linearlayoutandroid-xmlandroid-scrollview

解决方案


android:layout_marginBottom="-520dp"请从 ScrollView中删除负边距,然后尝试以下代码:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/MistyRose"
    android:orientation="vertical"
    android:paddingLeft="16dp"
    android:paddingTop="16dp"
    android:paddingRight="16dp"
    android:paddingBottom="16dp"   >

    <ImageView
        android:id="@+id/imageView2"
        android:layout_width="203dp"
        android:layout_height="95dp"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="50dp"
        android:background="@drawable/logo" />

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="70dp"
        android:layout_below="@id/imageView2"
        android:layout_centerHorizontal="true"
        android:text="Register"
        android:textColor="@color/Black"
        android:textSize="40dp"
        android:textStyle="italic" />

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@id/textView"
        android:fillViewport="true">

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

            <EditText
                android:id="@+id/Namebox"
                android:layout_width="300dp"
                android:layout_height="50dp"
                android:background="@drawable/edittext"
                android:layout_marginStart="30dp"
                android:ems="25"
                android:hint="Name"
                android:inputType="text"
                android:layout_marginLeft="30dp" />

            <EditText
                android:id="@+id/Enailbox"
                android:layout_width="300dp"
                android:layout_height="50dp"
                android:background="@drawable/edittext"
                android:layout_marginStart="30dp"
                android:layout_marginTop="30dp"
                android:hint="Email"
                android:inputType="textEmailAddress"
                android:layout_marginLeft="30dp" />

            <EditText
                android:id="@+id/Phonebox"
                android:layout_width="300dp"
                android:background="@drawable/edittext"
                android:layout_height="50dp"
                android:layout_marginStart="30dp"
                android:layout_marginTop="30dp"
                android:hint="Phone"
                android:inputType="phone"
                android:layout_marginLeft="30dp" />

            <EditText
                android:id="@+id/Passbox1"
                android:layout_width="300dp"
                android:background="@drawable/edittext"
                android:layout_height="50dp"
                android:layout_marginStart="30dp"
                android:layout_marginTop="30dp"
                android:hint="Password"
                android:inputType="textPassword"
                android:layout_marginLeft="30dp" />

            <EditText
                android:id="@+id/Passbox2"
                android:layout_width="300dp"
                android:background="@drawable/edittext"
                android:layout_height="50dp"
                android:layout_marginStart="30dp"
                android:layout_marginTop="30dp"
                android:hint="Password Confirm"
                android:inputType="textPassword"
                android:layout_marginLeft="30dp" />

            <Button
                android:layout_width="150dp"
                android:layout_height="50dp"
                android:background="@drawable/button"
                android:layout_marginStart="100dp"
                android:layout_marginTop="50dp"
                android:text="Submit"
                android:layout_marginLeft="100dp" />

        </LinearLayout>


    </ScrollView>

</RelativeLayout>

在您的 xml 代码中,您提供负边距,只需删除它们,您的布局将根据您的需要滚动。


推荐阅读