简体   繁体   English

ScrollView在相对布局中不起作用

[英]ScrollView not working in Relative Layout

I'm working on a Login page for my app . 我正在为我的应用程序登录页面。 The ScrollView isn't working and I'm unable to find out why. ScrollView无法正常工作,我无法找出原因。 I've tried changing the layout_height of ScrollView to wrap_content and some arbitrary value like 900dp and same with the relative layout inside but still no luck. 我试过将ScrollView的layout_height更改为wrap_content和一些任意值,例如900dp,并且与内部的相对布局相同,但仍然没有运气。 When the soft keyboard appears and I fill in details in say 1st editText view and want to scroll down to fill the second one without closing the Soft Keyboard. 当软键盘出现并且我在第一个editText视图中填写详细信息时,想要向下滚动以填充第二个而不关闭软键盘。 The page isn't scrolling. 该页面没有滚动。 Here is the Code 这是代码

<?xml version="1.0" encoding="utf-8"?>
<ScrollView
 xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="fill_parent"
 android:layout_height="fill_parent"
 android:fillViewport="true">

<RelativeLayout
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:paddingBottom="@dimen/activity_vertical_margin"
 android:paddingLeft="@dimen/activity_horizontal_margin"
 android:paddingRight="@dimen/activity_horizontal_margin"
 android:paddingTop="@dimen/activity_vertical_margin" >

<TextView
    android:id="@+id/register"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/textView4"
    android:layout_centerHorizontal="true"
    android:layout_marginBottom="14dp"
    android:linksClickable="true"
    android:text="New to scheduLAWyer ? Sign Up" />

<Button
    android:id="@+id/login"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/register"
    android:layout_alignParentLeft="true"
    android:layout_alignParentRight="true"
    android:layout_marginBottom="14dp"
    android:text="Login" />

<TextView
    android:id="@+id/textView4"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:layout_marginBottom="28dp"
    android:linksClickable="true"
    android:text="Forgot Password" />

<ImageView
    android:id="@+id/imageView123"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:src="@drawable/schedulawyer" />

<LinearLayout
    android:id="@+id/lll"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/login"
    android:layout_alignRight="@+id/login"
    android:layout_below="@+id/imageView123"
    android:background="@drawable/rounded_corner"
    android:orientation="vertical" >

    <EditText
        android:id="@+id/editText1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:hint="Email/Phone"
        android:singleLine="true" >

        <requestFocus />
    </EditText>

    <EditText
        android:id="@+id/editText2"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:hint="Password"
        android:inputType="textPassword" />
</LinearLayout>

</RelativeLayout>

</ScrollView>

Okay I solved it myself. 好吧,我自己解决了。 The problem was if an background image is added the page won't scroll until the image is bigger than the device size 问题是,如果添加了背景图像,则直到该图像大于设备尺寸时,页面才会滚动

Just wrap your RelativeLayout within one LinearLayout with android:layout_height="fill_parent" . 只需将您的RelativeLayoutandroid:layout_height="fill_parent"封装在一个LinearLayout android:layout_height="fill_parent" That will give the ScrollView content to expand to. 这将使ScrollView内容扩展到。

The other, somewhat awkward approach could be to do it programmatically -- as described in this answer . 另一种有点尴尬的方法可能是通过编程方式完成- 如此答案中所述

RelativeLayout的高度更改为wrap_content

<?xml version="1.0" encoding="UTF-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginBottom="15dp" >

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

// widgets afer this //此后的小部件

Try this and see what happens... 试试这个,看看会发生什么...

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin" >

    <ScrollView
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">

        <TextView
            android:id="@+id/register"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_above="@+id/textView4"
            android:layout_centerHorizontal="true"
            android:layout_marginBottom="14dp"
            android:linksClickable="true"
            android:text="New to scheduLAWyer ? Sign Up" />

        <Button
            android:id="@+id/login"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_above="@+id/register"
            android:layout_alignParentLeft="true"
            android:layout_alignParentRight="true"
            android:layout_marginBottom="14dp"
            android:text="Login" />

        <TextView
            android:id="@+id/textView4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_centerHorizontal="true"
            android:layout_marginBottom="28dp"
            android:linksClickable="true"
            android:text="Forgot Password" />

        <ImageView
            android:id="@+id/imageView123"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true"
            android:src="@drawable/schedulawyer" />

        <LinearLayout
            android:id="@+id/lll"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignLeft="@+id/login"
            android:layout_alignRight="@+id/login"
            android:layout_below="@+id/imageView123"
            android:background="@drawable/rounded_corner"
            android:orientation="vertical" >

        <EditText
            android:id="@+id/editText1"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:hint="Email/Phone"
            android:singleLine="true" >

            <requestFocus />
        </EditText>

        <EditText
            android:id="@+id/editText2"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:hint="Password"
            android:inputType="textPassword" />
        </LinearLayout>
    </ScrollView>
</RelativeLayout>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM