滚动视图中的相对布局无法滚动

4
我尝试将我的RelativeLayout用ScrollView包装以在横向模式下使用,但它不起作用。我也试过用LinearLayout包装我的RelativeLayout,但它也不起作用。
以下是RelativeLayout被包装在ScrollView中的XML代码:
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
    android:fillViewport="true">
<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#2d89ef">
    <!--Some components here-->
    </RelativeLayout>
    </ScrollView>

This my complete xml:

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
    android:fillViewport="true">
<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#2d89ef">

    <TextView
        android:id="@+id/textView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:fontFamily="Segoe UI Light"
        android:gravity="center"
        android:text="@string/app_name"
        android:textColor="#FFFFFF"
        android:textSize="25dp"
        android:textIsSelectable="false"
        android:layout_centerVertical="true"
        android:layout_alignParentLeft="true"
        android:layout_marginTop="10dp"
        android:layout_marginBottom="10dp" />

    <EditText
        android:id="@+id/editText"
        android:layout_width="275dp"
        android:layout_height="wrap_content"
        android:ems="10"
        android:fontFamily="Segoe UI Light"
        android:gravity="center_vertical"
        android:hint="@string/username"
        android:inputType="textPersonName"
        android:textColor="@android:color/white"
        android:textSize="16sp"
        android:width="200dp"
        android:layout_below="@+id/textView"
        android:layout_centerHorizontal="true">

        <requestFocus />
    </EditText>

    <EditText
        android:id="@+id/editText2"
        android:layout_width="250dp"
        android:layout_height="wrap_content"
        android:ems="10"
        android:fontFamily="Segoe UI Light"
        android:gravity="center_vertical"
        android:hint="@string/password"
        android:inputType="textPassword"
        android:textColor="@android:color/white"
        android:textSize="16sp"
        android:width="200dp"
        android:layout_below="@+id/editText"
        android:layout_alignRight="@+id/editText"
        android:layout_alignLeft="@+id/editText" />

    <Button
        android:id="@+id/button"
        android:layout_width="250dp"
        android:layout_height="40dp"
        android:background="@drawable/border"
        android:fontFamily="Segoe UI Light"
        android:height="20dp"
        android:text="@string/login"
        android:textColor="#FFFFFF"
        android:textSize="15sp"
        android:width="200dp"
        android:layout_marginTop="25dp"
        android:layout_below="@+id/editText2"
        android:layout_alignLeft="@+id/editText2"
        android:layout_alignRight="@+id/editText2" />

    <Button
        android:id="@id/btnForgot"
        android:layout_width="250dp"
        android:layout_height="40dp"
        android:background="@drawable/border"
        android:fontFamily="Segoe UI Light"
        android:height="20dp"
        android:text="@string/forgot_password"
        android:textColor="#FFFFFF"
        android:textSize="15sp"
        android:width="200dp"
        android:layout_below="@+id/button"
        android:layout_alignLeft="@+id/button"
        android:layout_alignRight="@+id/button" />

    <ImageView
        android:layout_width="140dp"
        android:layout_height="200dp"
        android:id="@+id/imageView"
        android:background="@drawable/auf_logo"
        android:layout_above="@+id/textView"
        android:layout_centerHorizontal="true" />

</RelativeLayout>
</ScrollView>

1
你添加了足够的内容以便能够滚动吗? - amalBit
是的,当我切换到横屏模式时,我无法看到一些按钮,因此我正在尝试使用ScrollView进行包装。 - dgzz
1
尝试这段代码,让我知道它是否对你有帮助?它对我有效。 - Shahil Modan
它确实能工作,但数据混乱了,你做了哪些更改? - dgzz
1
请检查我的答案。我已根据您的 XML 文件进行了更改。 - Sanket
显示剩余4条评论
2个回答

6

请查看我下面的代码...它与你的布局相同。所以只需替换它并检查它...

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

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#2d89ef"
    android:orientation="vertical" >

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

        <ImageView
            android:id="@+id/imageView"
            android:layout_width="140dp"
            android:layout_height="200dp"
            android:layout_centerHorizontal="true"
            android:background="@drawable/ic_launcher" />

        <TextView
            android:id="@+id/textView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_below="@id/imageView"
            android:layout_centerVertical="true"
            android:layout_marginBottom="10dp"
            android:layout_marginTop="10dp"
            android:fontFamily="Segoe UI Light"
            android:gravity="center"
            android:text="@string/app_name"
            android:textColor="#FFFFFF"
            android:textIsSelectable="false"
            android:textSize="25dp" />

        <EditText
            android:id="@+id/editText"
            android:layout_width="275dp"
            android:layout_height="wrap_content"
            android:layout_below="@+id/textView"
            android:layout_centerHorizontal="true"
            android:ems="10"
            android:fontFamily="Segoe UI Light"
            android:gravity="center_vertical"
            android:inputType="textPersonName"
            android:textColor="@android:color/white"
            android:textSize="16sp"
            android:width="200dp" >

            <requestFocus />
        </EditText>

        <EditText
            android:id="@+id/editText2"
            android:layout_width="250dp"
            android:layout_height="wrap_content"
            android:layout_alignLeft="@+id/editText"
            android:layout_alignRight="@+id/editText"
            android:layout_below="@+id/editText"
            android:ems="10"
            android:fontFamily="Segoe UI Light"
            android:gravity="center_vertical"
            android:inputType="textPassword"
            android:textColor="@android:color/white"
            android:textSize="16sp"
            android:width="200dp" />

        <Button
            android:id="@+id/button"
            android:layout_width="250dp"
            android:layout_height="40dp"
            android:layout_alignLeft="@+id/editText2"
            android:layout_alignRight="@+id/editText2"
            android:layout_below="@+id/editText2"
            android:layout_marginTop="25dp"
            android:fontFamily="Segoe UI Light"
            android:height="20dp"
            android:text="Login"
            android:textColor="#FFFFFF"
            android:textSize="15sp"
            android:width="200dp" />

        <Button
            android:id="@+id/btnForgot"
            android:layout_width="250dp"
            android:layout_height="40dp"
            android:layout_alignLeft="@+id/button"
            android:layout_alignRight="@+id/button"
            android:layout_below="@+id/button"
            android:fontFamily="Segoe UI Light"
            android:height="20dp"
            android:text="Forgot Password"
            android:textColor="#FFFFFF"
            android:textSize="15sp"
            android:width="200dp" />
    </RelativeLayout>
</LinearLayout>

</ScrollView>

滚动视图在哪里? - dgzz
2
我在ScrollView和RelativeLayout之间添加了LinearLayout,并重新布局视图,使UI与您的一样。 - Sanket
@Sanket,你能解释一下你的改动是如何修复问题的吗? - keerthana murugesan

0

试试这个...

     <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
     android:id="@+id/ScrollView01"
     android:layout_width="match_parent"
    android:layout_height="match_parent">

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

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

   <TextView
    android:id="@+id/textView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:fontFamily="Segoe UI Light"
    android:gravity="center"
    android:text="@string/app_name"
    android:textColor="#FFFFFF"
    android:textSize="25dp"
    android:textIsSelectable="false"
    android:layout_centerVertical="true"
    android:layout_alignParentLeft="true"
    android:layout_marginTop="10dp"
    android:layout_marginBottom="10dp" />

<EditText
    android:id="@+id/editText"
    android:layout_width="275dp"
    android:layout_height="wrap_content"
    android:ems="10"
    android:fontFamily="Segoe UI Light"
    android:gravity="center_vertical"
    android:hint="@string/username"
    android:inputType="textPersonName"
    android:textColor="@android:color/white"
    android:textSize="16sp"
    android:width="200dp"
    android:layout_below="@+id/textView"
    android:layout_centerHorizontal="true">

    <requestFocus />
</EditText>

<EditText
    android:id="@+id/editText2"
    android:layout_width="250dp"
    android:layout_height="wrap_content"
    android:ems="10"
    android:fontFamily="Segoe UI Light"
    android:gravity="center_vertical"
    android:hint="@string/password"
    android:inputType="textPassword"
    android:textColor="@android:color/white"
    android:textSize="16sp"
    android:width="200dp"
    android:layout_below="@+id/editText"
    android:layout_alignRight="@+id/editText"
    android:layout_alignLeft="@+id/editText" />

<Button
    android:id="@+id/button"
    android:layout_width="250dp"
    android:layout_height="40dp"
    android:background="@drawable/border"
    android:fontFamily="Segoe UI Light"
    android:height="20dp"
    android:text="@string/login"
    android:textColor="#FFFFFF"
    android:textSize="15sp"
    android:width="200dp"
    android:layout_marginTop="25dp"
    android:layout_below="@+id/editText2"
    android:layout_alignLeft="@+id/editText2"
    android:layout_alignRight="@+id/editText2" />

<Button
    android:id="@id/btnForgot"
    android:layout_width="250dp"
    android:layout_height="40dp"
    android:background="@drawable/border"
    android:fontFamily="Segoe UI Light"
    android:height="20dp"
    android:text="@string/forgot_password"
    android:textColor="#FFFFFF"
    android:textSize="15sp"
    android:width="200dp"
    android:layout_below="@+id/button"
    android:layout_alignLeft="@+id/button"
    android:layout_alignRight="@+id/button" />

<ImageView
    android:layout_width="140dp"
    android:layout_height="200dp"
    android:id="@+id/imageView"
    android:background="@drawable/auf_logo"
    android:layout_above="@+id/textView"
    android:layout_centerHorizontal="true" />

   </RelativeLayout>
    </LinearLayout>
   </ScrollView>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接